12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934 |
- using System;
- using System.Text;
- using UnityEngine;
- using FairyGUI.Utils;
- namespace FairyGUI
- {
- /// <summary>
- ///
- /// </summary>
- public class DisplayObject : EventDispatcher
- {
- /// <summary>
- ///
- /// </summary>
- public string name;
- /// <summary>
- ///
- /// </summary>
- public Container parent { get; private set; }
- /// <summary>
- ///
- /// </summary>
- public GameObject gameObject { get; protected set; }
- /// <summary>
- ///
- /// </summary>
- public Transform cachedTransform { get; protected set; }
- /// <summary>
- ///
- /// </summary>
- public NGraphics graphics { get; protected set; }
- /// <summary>
- ///
- /// </summary>
- public NGraphics paintingGraphics { get; protected set; }
- /// <summary>
- ///
- /// </summary>
- public event Action onPaint;
- /// <summary>
- ///
- /// </summary>
- public GObject gOwner;
- /// <summary>
- ///
- /// </summary>
- public uint id;
- bool _visible;
- bool _touchable;
- Vector2 _pivot;
- Vector3 _pivotOffset;
- Vector3 _rotation; //由于万向锁,单独旋转一个轴是会影响到其他轴的,所以这里需要单独保存
- Vector2 _skew;
- int _renderingOrder;
- float _alpha;
- bool _grayed;
- BlendMode _blendMode;
- IFilter _filter;
- Transform _home;
- string _cursor;
- bool _perspective;
- int _focalLength;
- Vector3 _pixelPerfectAdjustment;
- int _checkPixelPerfect;
- EventListener _onClick;
- EventListener _onRightClick;
- EventListener _onTouchBegin;
- EventListener _onTouchMove;
- EventListener _onTouchEnd;
- EventListener _onRollOver;
- EventListener _onRollOut;
- EventListener _onMouseWheel;
- EventListener _onAddedToStage;
- EventListener _onRemovedFromStage;
- EventListener _onKeyDown;
- EventListener _onClickLink;
- EventListener _onFocusIn;
- EventListener _onFocusOut;
- protected internal int _paintingMode; //1-滤镜,2-blendMode,4-transformMatrix, 8-cacheAsBitmap
- protected internal PaintingInfo _paintingInfo;
- protected Rect _contentRect;
- protected NGraphics.VertexMatrix _vertexMatrix;
- protected internal Flags _flags;
- protected internal float[] _batchingBounds;
- internal static uint _gInstanceCounter;
- internal static HideFlags hideFlags = HideFlags.None;
- public DisplayObject()
- {
- id = _gInstanceCounter++;
- _alpha = 1;
- _visible = true;
- _touchable = true;
- _blendMode = BlendMode.Normal;
- _focalLength = 2000;
- _flags |= Flags.OutlineChanged;
- if (UIConfig.makePixelPerfect)
- _flags |= Flags.PixelPerfect;
- }
- /// <summary>
- ///
- /// </summary>
- public EventListener onClick
- {
- get { return _onClick ?? (_onClick = new EventListener(this, "onClick")); }
- }
- /// <summary>
- ///
- /// </summary>
- public EventListener onRightClick
- {
- get { return _onRightClick ?? (_onRightClick = new EventListener(this, "onRightClick")); }
- }
- /// <summary>
- ///
- /// </summary>
- public EventListener onTouchBegin
- {
- get { return _onTouchBegin ?? (_onTouchBegin = new EventListener(this, "onTouchBegin")); }
- }
- /// <summary>
- ///
- /// </summary>
- public EventListener onTouchMove
- {
- get { return _onTouchMove ?? (_onTouchMove = new EventListener(this, "onTouchMove")); }
- }
- /// <summary>
- ///
- /// </summary>
- public EventListener onTouchEnd
- {
- get { return _onTouchEnd ?? (_onTouchEnd = new EventListener(this, "onTouchEnd")); }
- }
- /// <summary>
- ///
- /// </summary>
- public EventListener onRollOver
- {
- get { return _onRollOver ?? (_onRollOver = new EventListener(this, "onRollOver")); }
- }
- /// <summary>
- ///
- /// </summary>
- public EventListener onRollOut
- {
- get { return _onRollOut ?? (_onRollOut = new EventListener(this, "onRollOut")); }
- }
- /// <summary>
- ///
- /// </summary>
- public EventListener onMouseWheel
- {
- get { return _onMouseWheel ?? (_onMouseWheel = new EventListener(this, "onMouseWheel")); }
- }
- /// <summary>
- ///
- /// </summary>
- public EventListener onAddedToStage
- {
- get { return _onAddedToStage ?? (_onAddedToStage = new EventListener(this, "onAddedToStage")); }
- }
- /// <summary>
- ///
- /// </summary>
- public EventListener onRemovedFromStage
- {
- get { return _onRemovedFromStage ?? (_onRemovedFromStage = new EventListener(this, "onRemovedFromStage")); }
- }
- /// <summary>
- ///
- /// </summary>
- public EventListener onKeyDown
- {
- get { return _onKeyDown ?? (_onKeyDown = new EventListener(this, "onKeyDown")); }
- }
- /// <summary>
- ///
- /// </summary>
- public EventListener onClickLink
- {
- get { return _onClickLink ?? (_onClickLink = new EventListener(this, "onClickLink")); }
- }
- /// <summary>
- ///
- /// </summary>
- public EventListener onFocusIn
- {
- get { return _onFocusIn ?? (_onFocusIn = new EventListener(this, "onFocusIn")); }
- }
- /// <summary>
- ///
- /// </summary>
- public EventListener onFocusOut
- {
- get { return _onFocusOut ?? (_onFocusOut = new EventListener(this, "onFocusOut")); }
- }
- protected void CreateGameObject(string gameObjectName)
- {
- gameObject = new GameObject(gameObjectName);
- cachedTransform = gameObject.transform;
- if (Application.isPlaying)
- {
- UnityEngine.Object.DontDestroyOnLoad(gameObject);
- DisplayObjectInfo info = gameObject.AddComponent<DisplayObjectInfo>();
- info.displayObject = this;
- }
- gameObject.hideFlags = DisplayObject.hideFlags;
- gameObject.SetActive(false);
- }
- protected void SetGameObject(GameObject gameObject)
- {
- this.gameObject = gameObject;
- this.cachedTransform = gameObject.transform;
- _rotation = cachedTransform.localEulerAngles;
- _flags |= Flags.UserGameObject;
- }
- protected void DestroyGameObject()
- {
- if ((_flags & Flags.UserGameObject) == 0 && gameObject != null)
- {
- if (Application.isPlaying)
- GameObject.Destroy(gameObject);
- else
- GameObject.DestroyImmediate(gameObject);
- gameObject = null;
- cachedTransform = null;
- }
- }
- /// <summary>
- ///
- /// </summary>
- public float alpha
- {
- get { return _alpha; }
- set { _alpha = value; }
- }
- /// <summary>
- ///
- /// </summary>
- public bool grayed
- {
- get { return _grayed; }
- set { _grayed = value; }
- }
- /// <summary>
- ///
- /// </summary>
- public bool visible
- {
- get { return _visible; }
- set
- {
- if (_visible != value)
- {
- _visible = value;
- _flags |= Flags.OutlineChanged;
- if (parent != null && _visible)
- {
- gameObject.SetActive(true);
- InvalidateBatchingState();
- if (this is Container)
- ((Container)this).InvalidateBatchingState(true);
- }
- else
- gameObject.SetActive(false);
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- public float x
- {
- get { return cachedTransform.localPosition.x; }
- set
- {
- SetPosition(value, -cachedTransform.localPosition.y, cachedTransform.localPosition.z);
- }
- }
- /// <summary>
- ///
- /// </summary>
- public float y
- {
- get { return -cachedTransform.localPosition.y; }
- set
- {
- SetPosition(cachedTransform.localPosition.x, value, cachedTransform.localPosition.z);
- }
- }
- /// <summary>
- ///
- /// </summary>
- public float z
- {
- get { return cachedTransform.localPosition.z; }
- set
- {
- SetPosition(cachedTransform.localPosition.x, -cachedTransform.localPosition.y, value);
- }
- }
- /// <summary>
- ///
- /// </summary>
- public Vector2 xy
- {
- get { return new Vector2(this.x, this.y); }
- set { SetPosition(value.x, value.y, cachedTransform.localPosition.z); }
- }
- /// <summary>
- ///
- /// </summary>
- public Vector3 position
- {
- get { return new Vector3(this.x, this.y, this.z); }
- set { SetPosition(value.x, value.y, value.z); }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="xv"></param>
- /// <param name="yv"></param>
- public void SetXY(float xv, float yv)
- {
- SetPosition(xv, yv, cachedTransform.localPosition.z);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="xv"></param>
- /// <param name="yv"></param>
- /// <param name="zv"></param>
- public void SetPosition(float xv, float yv, float zv)
- {
- Vector3 v = new Vector3();
- v.x = xv;
- v.y = -yv;
- v.z = zv;
- if (v != cachedTransform.localPosition)
- {
- cachedTransform.localPosition = v;
- _flags |= Flags.OutlineChanged;
- if ((_flags & Flags.PixelPerfect) != 0)
- {
- //总在下一帧再完成PixelPerfect,这样当物体在连续运动时,不会因为PixelPerfect而发生抖动。
- _checkPixelPerfect = Time.frameCount;
- _pixelPerfectAdjustment = Vector3.zero;
- }
- }
- }
- /// <summary>
- /// If the object position is align by pixel
- /// </summary>
- public bool pixelPerfect
- {
- get { return (_flags & Flags.PixelPerfect) != 0; }
- set
- {
- if (value)
- _flags |= Flags.PixelPerfect;
- else
- _flags &= ~Flags.PixelPerfect;
- }
- }
- /// <summary>
- ///
- /// </summary>
- public float width
- {
- get
- {
- EnsureSizeCorrect();
- return _contentRect.width;
- }
- set
- {
- if (!Mathf.Approximately(value, _contentRect.width))
- {
- _contentRect.width = value;
- _flags |= Flags.WidthChanged;
- _flags &= ~Flags.HeightChanged;
- OnSizeChanged();
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- public float height
- {
- get
- {
- EnsureSizeCorrect();
- return _contentRect.height;
- }
- set
- {
- if (!Mathf.Approximately(value, _contentRect.height))
- {
- _contentRect.height = value;
- _flags &= ~Flags.WidthChanged;
- _flags |= Flags.HeightChanged;
- OnSizeChanged();
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- public Vector2 size
- {
- get
- {
- EnsureSizeCorrect();
- return _contentRect.size;
- }
- set
- {
- SetSize(value.x, value.y);
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="wv"></param>
- /// <param name="hv"></param>
- public void SetSize(float wv, float hv)
- {
- if (!Mathf.Approximately(wv, _contentRect.width))
- _flags |= Flags.WidthChanged;
- else
- _flags &= ~Flags.WidthChanged;
- if (!Mathf.Approximately(hv, _contentRect.height))
- _flags |= Flags.HeightChanged;
- else
- _flags &= ~Flags.HeightChanged;
- if ((_flags & Flags.WidthChanged) != 0 || (_flags & Flags.HeightChanged) != 0)
- {
- _contentRect.width = wv;
- _contentRect.height = hv;
- OnSizeChanged();
- }
- }
- virtual public void EnsureSizeCorrect()
- {
- }
- virtual protected void OnSizeChanged()
- {
- ApplyPivot();
- if (_paintingInfo != null)
- _paintingInfo.flag = 1;
- if (graphics != null)
- graphics.contentRect = _contentRect;
- _flags |= Flags.OutlineChanged;
- }
- /// <summary>
- ///
- /// </summary>
- public float scaleX
- {
- get { return cachedTransform.localScale.x; }
- set
- {
- Vector3 v = cachedTransform.localScale;
- v.x = v.z = ValidateScale(value);
- cachedTransform.localScale = v;
- _flags |= Flags.OutlineChanged;
- ApplyPivot();
- }
- }
- /// <summary>
- ///
- /// </summary>
- public float scaleY
- {
- get { return cachedTransform.localScale.y; }
- set
- {
- Vector3 v = cachedTransform.localScale;
- v.y = ValidateScale(value);
- cachedTransform.localScale = v;
- _flags |= Flags.OutlineChanged;
- ApplyPivot();
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="xv"></param>
- /// <param name="yv"></param>
- public void SetScale(float xv, float yv)
- {
- Vector3 v = new Vector3();
- v.x = v.z = ValidateScale(xv);
- v.y = ValidateScale(yv);
- cachedTransform.localScale = v;
- _flags |= Flags.OutlineChanged;
- ApplyPivot();
- }
- /// <summary>
- /// 在scale过小情况(极端情况=0),当使用Transform的坐标变换时,变换到世界,再从世界变换到本地,会由于精度问题造成结果错误。
- /// 这种错误会导致Batching错误,因为Batching会使用缓存的outline。
- /// 这里限制一下scale的最小值作为当前解决方案。
- /// 这个方案并不完美,因为限制了本地scale值并不能保证对世界scale不会过小。
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- private float ValidateScale(float value)
- {
- if (value >= 0 && value < 0.001f)
- value = 0.001f;
- else if (value < 0 && value > -0.001f)
- value = -0.001f;
- return value;
- }
- /// <summary>
- ///
- /// </summary>
- public Vector2 scale
- {
- get { return cachedTransform.localScale; }
- set
- {
- SetScale(value.x, value.y);
- }
- }
- /// <summary>
- ///
- /// </summary>
- public float rotation
- {
- get
- {
- //和Unity默认的旋转方向相反
- return -_rotation.z;
- }
- set
- {
- _rotation.z = -value;
- _flags |= Flags.OutlineChanged;
- if (_perspective)
- UpdateTransformMatrix();
- else
- {
- cachedTransform.localEulerAngles = _rotation;
- ApplyPivot();
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- public float rotationX
- {
- get
- {
- return _rotation.x;
- }
- set
- {
- _rotation.x = value;
- _flags |= Flags.OutlineChanged;
- if (_perspective)
- UpdateTransformMatrix();
- else
- {
- cachedTransform.localEulerAngles = _rotation;
- ApplyPivot();
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- public float rotationY
- {
- get
- {
- return _rotation.y;
- }
- set
- {
- _rotation.y = value;
- _flags |= Flags.OutlineChanged;
- if (_perspective)
- UpdateTransformMatrix();
- else
- {
- cachedTransform.localEulerAngles = _rotation;
- ApplyPivot();
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- public Vector2 skew
- {
- get { return _skew; }
- set
- {
- _skew = value;
- _flags |= Flags.OutlineChanged;
- if (!Application.isPlaying) //编辑期间不支持!!
- return;
- UpdateTransformMatrix();
- }
- }
- /// <summary>
- /// 当对象处于ScreenSpace,也就是使用正交相机渲染时,对象虽然可以绕X轴或者Y轴旋转,但没有透视效果。设置perspective,可以模拟出透视效果。
- /// </summary>
- public bool perspective
- {
- get
- {
- return _perspective;
- }
- set
- {
- if (_perspective != value)
- {
- _perspective = value;
- if (_perspective)//屏蔽Unity自身的旋转变换
- cachedTransform.localEulerAngles = Vector3.zero;
- else
- cachedTransform.localEulerAngles = _rotation;
- ApplyPivot();
- UpdateTransformMatrix();
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- public int focalLength
- {
- get { return _focalLength; }
- set
- {
- if (value <= 0)
- value = 1;
- _focalLength = value;
- if (_vertexMatrix != null)
- UpdateTransformMatrix();
- }
- }
- void UpdateTransformMatrix()
- {
- Matrix4x4 matrix = Matrix4x4.identity;
- if (_skew.x != 0 || _skew.y != 0)
- ToolSet.SkewMatrix(ref matrix, _skew.x, _skew.y);
- if (_perspective)
- matrix *= Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(_rotation), Vector3.one);
- if (matrix.isIdentity)
- _vertexMatrix = null;
- else if (_vertexMatrix == null)
- _vertexMatrix = new NGraphics.VertexMatrix();
- //组件的transformMatrix是通过paintingMode实现的,因为全部通过矩阵变换的话,和unity自身的变换混杂在一起,无力理清。
- if (_vertexMatrix != null)
- {
- _vertexMatrix.matrix = matrix;
- _vertexMatrix.cameraPos = new Vector3(_pivot.x * _contentRect.width, -_pivot.y * _contentRect.height, _focalLength);
- if (graphics == null)
- EnterPaintingMode(4, null);
- }
- else
- {
- if (graphics == null)
- LeavePaintingMode(4);
- }
- if (_paintingMode > 0)
- {
- paintingGraphics.vertexMatrix = _vertexMatrix;
- _paintingInfo.flag = 1;
- }
- else if (graphics != null)
- graphics.vertexMatrix = _vertexMatrix;
- _flags |= Flags.OutlineChanged;
- }
- /// <summary>
- ///
- /// </summary>
- public Vector2 pivot
- {
- get { return _pivot; }
- set
- {
- Vector3 deltaPivot = new Vector2((value.x - _pivot.x) * _contentRect.width, (_pivot.y - value.y) * _contentRect.height);
- Vector3 oldOffset = _pivotOffset;
- _pivot = value;
- UpdatePivotOffset();
- Vector3 v = cachedTransform.localPosition;
- v += oldOffset - _pivotOffset + deltaPivot;
- cachedTransform.localPosition = v;
- _flags |= Flags.OutlineChanged;
- }
- }
- void UpdatePivotOffset()
- {
- float px = _pivot.x * _contentRect.width;
- float py = _pivot.y * _contentRect.height;
- //注意这里不用处理skew,因为在顶点变换里有对pivot的处理
- Matrix4x4 matrix = Matrix4x4.TRS(Vector3.zero, cachedTransform.localRotation, cachedTransform.localScale);
- _pivotOffset = matrix.MultiplyPoint(new Vector3(px, -py, 0));
- if (_vertexMatrix != null)
- _vertexMatrix.cameraPos = new Vector3(_pivot.x * _contentRect.width, -_pivot.y * _contentRect.height, _focalLength);
- }
- void ApplyPivot()
- {
- if (_pivot.x != 0 || _pivot.y != 0)
- {
- Vector3 oldOffset = _pivotOffset;
- UpdatePivotOffset();
- Vector3 v = cachedTransform.localPosition;
- if ((_flags & Flags.PixelPerfect) != 0)
- {
- v -= _pixelPerfectAdjustment;
- _checkPixelPerfect = Time.frameCount;
- _pixelPerfectAdjustment = Vector3.zero;
- }
- v += oldOffset - _pivotOffset;
- cachedTransform.localPosition = v;
- _flags |= Flags.OutlineChanged;
- }
- }
- /// <summary>
- /// This is the pivot position
- /// </summary>
- public Vector3 location
- {
- get
- {
- Vector3 pos = this.position;
- pos.x += _pivotOffset.x;
- pos.y -= _pivotOffset.y;
- pos.z += _pivotOffset.z;
- return pos;
- }
- set
- {
- this.SetPosition(value.x - _pivotOffset.x, value.y + _pivotOffset.y, value.z - _pivotOffset.z);
- }
- }
- /// <summary>
- ///
- /// </summary>
- virtual public Material material
- {
- get
- {
- if (graphics != null)
- return graphics.material;
- else
- return null;
- }
- set
- {
- if (graphics != null)
- graphics.material = value;
- }
- }
- /// <summary>
- ///
- /// </summary>
- virtual public string shader
- {
- get
- {
- if (graphics != null)
- return graphics.shader;
- else
- return null;
- }
- set
- {
- if (graphics != null)
- graphics.shader = value;
- }
- }
- /// <summary>
- ///
- /// </summary>
- virtual public int renderingOrder
- {
- get
- {
- return _renderingOrder;
- }
- set
- {
- if ((_flags & Flags.GameObjectDisposed) != 0)
- {
- DisplayDisposedWarning();
- return;
- }
- _renderingOrder = value;
- if (graphics != null)
- graphics.sortingOrder = value;
- if (_paintingMode > 0)
- paintingGraphics.sortingOrder = value;
- }
- }
- /// <summary>
- ///
- /// </summary>
- public int layer
- {
- get
- {
- if (_paintingMode > 0)
- return paintingGraphics.gameObject.layer;
- else
- return gameObject.layer;
- }
- set
- {
- SetLayer(value, false);
- }
- }
- /// <summary>
- /// If the object can be focused?
- /// </summary>
- public bool focusable
- {
- get { return (_flags & Flags.NotFocusable) == 0; }
- set
- {
- if (value)
- _flags &= ~Flags.NotFocusable;
- else
- _flags |= Flags.NotFocusable;
- }
- }
- /// <summary>
- /// If the object can be navigated by TAB?
- /// </summary>
- public bool tabStop
- {
- get { return (_flags & Flags.TabStop) != 0; }
- set
- {
- if (value)
- _flags |= Flags.TabStop;
- else
- _flags &= ~Flags.TabStop;
- }
- }
- /// <summary>
- /// If the object focused?
- /// </summary>
- public bool focused
- {
- get
- {
- return Stage.inst.focus == this || (this is Container) && ((Container)this).IsAncestorOf(Stage.inst.focus);
- }
- }
- internal bool _AcceptTab()
- {
- if (_touchable && _visible
- && ((_flags & Flags.TabStop) != 0 || (_flags & Flags.TabStopChildren) != 0)
- && (_flags & Flags.NotFocusable) == 0)
- {
- Stage.inst.SetFocus(this, true);
- return true;
- }
- else
- return false;
- }
- /// <summary>
- ///
- /// </summary>
- /// <value></value>
- public string cursor
- {
- get { return _cursor; }
- set
- {
- _cursor = value;
- if (Application.isPlaying
- && (this == Stage.inst.touchTarget || (this is Container) && ((Container)this).IsAncestorOf(Stage.inst.touchTarget)))
- {
- Stage.inst._ChangeCursor(_cursor);
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- public bool isDisposed
- {
- get { return (_flags & Flags.Disposed) != 0 || gameObject == null; }
- }
- internal void InternalSetParent(Container value)
- {
- if (parent != value)
- {
- if (value == null && (parent._flags & Flags.Disposed) != 0)
- parent = value;
- else
- {
- parent = value;
- UpdateHierarchy();
- }
- _flags |= Flags.OutlineChanged;
- }
- }
- /// <summary>
- ///
- /// </summary>
- public Container topmost
- {
- get
- {
- DisplayObject currentObject = this;
- while (currentObject.parent != null)
- currentObject = currentObject.parent;
- return currentObject as Container;
- }
- }
- /// <summary>
- ///
- /// </summary>
- public Stage stage
- {
- get
- {
- return topmost as Stage;
- }
- }
- /// <summary>
- ///
- /// </summary>
- public Container worldSpaceContainer
- {
- get
- {
- Container wsc = null;
- DisplayObject currentObject = this;
- while (currentObject.parent != null)
- {
- if ((currentObject is Container) && ((Container)currentObject).renderMode == RenderMode.WorldSpace)
- {
- wsc = (Container)currentObject;
- break;
- }
- currentObject = currentObject.parent;
- }
- return wsc;
- }
- }
- /// <summary>
- ///
- /// </summary>
- public bool touchable
- {
- get { return _touchable; }
- set
- {
- if (_touchable != value)
- {
- _touchable = value;
- if (this is Container)
- {
- ColliderHitTest hitArea = ((Container)this).hitArea as ColliderHitTest;
- if (hitArea != null)
- hitArea.collider.enabled = value;
- }
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <value></value>
- public bool touchDisabled
- {
- get { return (_flags & Flags.TouchDisabled) != 0; }
- }
- /// <summary>
- /// 进入绘画模式,整个对象将画到一张RenderTexture上,然后这种贴图将代替原有的显示内容。
- /// 可以在onPaint回调里对这张纹理进行进一步操作,实现特殊效果。
- /// </summary>
- public void EnterPaintingMode()
- {
- EnterPaintingMode(16384, null, 1);
- }
- /// <summary>
- /// 进入绘画模式,整个对象将画到一张RenderTexture上,然后这种贴图将代替原有的显示内容。
- /// 可以在onPaint回调里对这张纹理进行进一步操作,实现特殊效果。
- /// 可能有多个地方要求进入绘画模式,这里用requestorId加以区别,取值是1、2、4、8、16以此类推。1024内内部保留。用户自定义的id从1024开始。
- /// </summary>
- /// <param name="requestId">请求者id</param>
- /// <param name="extend">纹理四周的留空。如果特殊处理后的内容大于原内容,那么这里的设置可以使纹理扩大。</param>
- public void EnterPaintingMode(int requestorId, Margin? extend)
- {
- EnterPaintingMode(requestorId, extend, 1);
- }
- /// <summary>
- /// 进入绘画模式,整个对象将画到一张RenderTexture上,然后这种贴图将代替原有的显示内容。
- /// 可以在onPaint回调里对这张纹理进行进一步操作,实现特殊效果。
- /// 可能有多个地方要求进入绘画模式,这里用requestorId加以区别,取值是1、2、4、8、16以此类推。1024内内部保留。用户自定义的id从1024开始。
- /// </summary>
- /// <param name="requestorId">请求者id</param>
- /// <param name="extend">扩展纹理。如果特殊处理后的内容大于原内容,那么这里的设置可以使纹理扩大。</param>
- /// <param name="scale">附加一个缩放系数</param>
- public void EnterPaintingMode(int requestorId, Margin? extend, float scale)
- {
- bool first = _paintingMode == 0;
- _paintingMode |= requestorId;
- if (first)
- {
- if (_paintingInfo == null)
- {
- _paintingInfo = new PaintingInfo()
- {
- captureDelegate = Capture,
- scale = 1
- };
- }
- if (paintingGraphics == null)
- {
- if (graphics == null)
- paintingGraphics = new NGraphics(this.gameObject);
- else
- {
- GameObject go = new GameObject(this.gameObject.name + " (Painter)");
- go.layer = this.gameObject.layer;
- go.transform.SetParent(cachedTransform, false);
- go.hideFlags = DisplayObject.hideFlags;
- paintingGraphics = new NGraphics(go);
- }
- }
- else
- paintingGraphics.enabled = true;
- paintingGraphics.vertexMatrix = null;
- if (this is Container)
- {
- ((Container)this).SetChildrenLayer(CaptureCamera.hiddenLayer);
- ((Container)this).UpdateBatchingFlags();
- }
- else
- this.InvalidateBatchingState();
- if (graphics != null)
- this.gameObject.layer = CaptureCamera.hiddenLayer;
- }
- if (extend != null)
- _paintingInfo.extend = (Margin)extend;
- _paintingInfo.scale = scale;
- _paintingInfo.flag = 1;
- }
- /// <summary>
- /// 离开绘画模式
- /// </summary>
- /// <param name="requestId"></param>
- public void LeavePaintingMode(int requestorId)
- {
- if (_paintingMode == 0 || (_flags & Flags.Disposed) != 0)
- return;
- _paintingMode ^= requestorId;
- if (_paintingMode == 0)
- {
- paintingGraphics.enabled = false;
- if (this is Container)
- {
- ((Container)this).SetChildrenLayer(this.layer);
- ((Container)this).UpdateBatchingFlags();
- }
- else
- this.InvalidateBatchingState();
- if (graphics != null)
- this.gameObject.layer = paintingGraphics.gameObject.layer;
- }
- }
- /// <summary>
- ///
- /// </summary>
- public bool paintingMode
- {
- get { return _paintingMode > 0; }
- }
- /// <summary>
- /// 将整个显示对象(如果是容器,则容器包含的整个显示列表)静态化,所有内容被缓冲到一张纹理上。
- /// DC将保持为1。CPU消耗将降到最低。但对象的任何变化不会更新。
- /// 当cacheAsBitmap已经为true时,再次调用cacheAsBitmap=true将会刷新对象一次。
- /// </summary>
- public bool cacheAsBitmap
- {
- get { return (_flags & Flags.CacheAsBitmap) != 0; }
- set
- {
- if (value)
- {
- _flags |= Flags.CacheAsBitmap;
- EnterPaintingMode(8, null, UIContentScaler.scaleFactor);
- }
- else
- {
- _flags &= ~Flags.CacheAsBitmap;
- LeavePaintingMode(8);
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="extend"></param>
- /// <param name="scale"></param>
- /// <returns></returns>
- public Texture2D GetScreenShot(Margin? extend, float scale)
- {
- EnterPaintingMode(8, null, scale);
- UpdatePainting();
- Capture();
- Texture2D output;
- if (paintingGraphics.texture == null)
- output = new Texture2D(1, 1, TextureFormat.RGBA32, false, true);
- else
- {
- RenderTexture rt = (RenderTexture)paintingGraphics.texture.nativeTexture;
- output = new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, false, true);
- RenderTexture old = RenderTexture.active;
- RenderTexture.active = rt;
- output.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
- output.Apply();
- RenderTexture.active = old;
- }
- LeavePaintingMode(8);
- return output;
- }
- /// <summary>
- ///
- /// </summary>
- public IFilter filter
- {
- get
- {
- return _filter;
- }
- set
- {
- if (!Application.isPlaying) //编辑期间不支持!!
- return;
- if (value == _filter)
- return;
- if (_filter != null)
- _filter.Dispose();
- if (value != null && value.target != null)
- value.target.filter = null;
- _filter = value;
- if (_filter != null)
- _filter.target = this;
- }
- }
- /// <summary>
- ///
- /// </summary>
- public BlendMode blendMode
- {
- get { return _blendMode; }
- set
- {
- _blendMode = value;
- InvalidateBatchingState();
- if (graphics == null)
- {
- if (_blendMode != BlendMode.Normal)
- {
- if (!Application.isPlaying) //Not supported in edit mode!
- return;
- EnterPaintingMode(2, null);
- paintingGraphics.blendMode = _blendMode;
- }
- else
- LeavePaintingMode(2);
- }
- else
- graphics.blendMode = _blendMode;
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="targetSpace"></param>
- /// <returns></returns>
- virtual public Rect GetBounds(DisplayObject targetSpace)
- {
- EnsureSizeCorrect();
- if (targetSpace == this) // optimization
- {
- return _contentRect;
- }
- else if (targetSpace == parent && _rotation.z == 0)
- {
- return new Rect(cachedTransform.localPosition.x, -cachedTransform.localPosition.y,
- _contentRect.width * cachedTransform.localScale.x, _contentRect.height * cachedTransform.localScale.y);
- }
- else
- return TransformRect(_contentRect, targetSpace);
- }
- internal DisplayObject InternalHitTest()
- {
- if (_visible && (!HitTestContext.forTouch || _touchable))
- return HitTest();
- else
- return null;
- }
- internal DisplayObject InternalHitTestMask()
- {
- if (_visible)
- return HitTest();
- else
- return null;
- }
- virtual protected DisplayObject HitTest()
- {
- Rect rect = GetBounds(this);
- if (rect.width == 0 || rect.height == 0)
- return null;
- Vector2 localPoint = WorldToLocal(HitTestContext.worldPoint, HitTestContext.direction);
- if (rect.Contains(localPoint))
- return this;
- else
- return null;
- }
- /// <summary>
- /// 将舞台坐标转换为本地坐标
- /// </summary>
- /// <param name="point"></param>
- /// <returns></returns>
- public Vector2 GlobalToLocal(Vector2 point)
- {
- Container wsc = this.worldSpaceContainer;
- if (wsc != null)//I am in a world space
- {
- Camera cam = wsc.GetRenderCamera();
- Vector3 worldPoint;
- Vector3 direction;
- Vector3 screenPoint = new Vector3();
- screenPoint.x = point.x;
- screenPoint.y = Screen.height - point.y;
- if (wsc.hitArea is MeshColliderHitTest)
- {
- Ray ray = cam.ScreenPointToRay(screenPoint);
- RaycastHit hit;
- if (((MeshColliderHitTest)wsc.hitArea).collider.Raycast(ray, out hit, 100))
- {
- point = new Vector2(hit.textureCoord.x * _contentRect.width, (1 - hit.textureCoord.y) * _contentRect.height);
- worldPoint = Stage.inst.cachedTransform.TransformPoint(point.x, -point.y, 0);
- direction = Vector3.back;
- }
- else //当射线没有击中模型时,无法确定本地坐标
- return new Vector2(float.NaN, float.NaN);
- }
- else
- {
- screenPoint.z = cam.WorldToScreenPoint(this.cachedTransform.position).z;
- worldPoint = cam.ScreenToWorldPoint(screenPoint);
- Ray ray = cam.ScreenPointToRay(screenPoint);
- direction = Vector3.zero - ray.direction;
- }
- return this.WorldToLocal(worldPoint, direction);
- }
- else //I am in stage space
- {
- Vector3 worldPoint = Stage.inst.cachedTransform.TransformPoint(point.x, -point.y, 0);
- return this.WorldToLocal(worldPoint, Vector3.back);
- }
- }
- /// <summary>
- /// 将本地坐标转换为舞台坐标
- /// </summary>
- /// <param name="point"></param>
- /// <returns></returns>
- public Vector2 LocalToGlobal(Vector2 point)
- {
- Container wsc = this.worldSpaceContainer;
- Vector3 worldPoint = this.cachedTransform.TransformPoint(point.x, -point.y, 0);
- if (wsc != null)
- {
- if (wsc.hitArea is MeshColliderHitTest) //Not supported for UIPainter, use TransfromPoint instead.
- return new Vector2(float.NaN, float.NaN);
- Vector3 screePoint = wsc.GetRenderCamera().WorldToScreenPoint(worldPoint);
- return new Vector2(screePoint.x, Stage.inst.size.y - screePoint.y);
- }
- else
- {
- point = Stage.inst.cachedTransform.InverseTransformPoint(worldPoint);
- point.y = -point.y;
- return point;
- }
- }
- /// <summary>
- /// 转换世界坐标点到等效的本地xy平面的点。等效的意思是他们在屏幕方向看到的位置一样。
- /// 返回的点是在对象的本地坐标空间,且z=0
- /// </summary>
- /// <param name="worldPoint"></param>
- /// <param name="direction"></param>
- /// <returns></returns>
- public Vector3 WorldToLocal(Vector3 worldPoint, Vector3 direction)
- {
- Vector3 localPoint = this.cachedTransform.InverseTransformPoint(worldPoint);
- if (localPoint.z != 0) //如果对象绕x轴或y轴旋转过,或者对象是在透视相机,那么z值可能不为0,
- {
- //将世界坐标的摄影机方向在本地空间上投射,求出与xy平面的交点
- direction = this.cachedTransform.InverseTransformDirection(direction);
- float distOnLine = Vector3.Dot(Vector3.zero - localPoint, Vector3.forward) / Vector3.Dot(direction, Vector3.forward);
- if (float.IsInfinity(distOnLine))
- return Vector2.zero;
- localPoint = localPoint + direction * distOnLine;
- }
- else if (_vertexMatrix != null)
- {
- Vector3 center = _vertexMatrix.cameraPos;
- center.z = 0;
- center -= _vertexMatrix.matrix.MultiplyPoint(center);
- Matrix4x4 mm = _vertexMatrix.matrix.inverse;
- localPoint -= center;
- localPoint = mm.MultiplyPoint(localPoint);
- Vector3 camPos = mm.MultiplyPoint(_vertexMatrix.cameraPos);
- Vector3 vec = localPoint - camPos;
- float lambda = -camPos.z / vec.z;
- localPoint = camPos + lambda * vec;
- localPoint.z = 0;
- }
- localPoint.y = -localPoint.y;
- return localPoint;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="localPoint"></param>
- /// <returns></returns>
- public Vector3 LocalToWorld(Vector3 localPoint)
- {
- localPoint.y = -localPoint.y;
- if (_vertexMatrix != null)
- {
- Vector3 center = _vertexMatrix.cameraPos;
- center.z = 0;
- center -= _vertexMatrix.matrix.MultiplyPoint(center);
- localPoint = _vertexMatrix.matrix.MultiplyPoint(localPoint);
- localPoint += center;
- Vector3 camPos = _vertexMatrix.cameraPos;
- Vector3 vec = localPoint - camPos;
- float lambda = -camPos.z / vec.z;
- localPoint = camPos + lambda * vec;
- localPoint.z = 0;
- }
- return this.cachedTransform.TransformPoint(localPoint);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="point"></param>
- /// <param name="targetSpace">null if to world space</param>
- /// <returns></returns>
- public Vector2 TransformPoint(Vector2 point, DisplayObject targetSpace)
- {
- if (targetSpace == this)
- return point;
- point = LocalToWorld(point);
- if (targetSpace != null)
- point = targetSpace.WorldToLocal(point, Vector3.back);
- return point;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="rect"></param>
- /// <param name="targetSpace">null if to world space</param>
- /// <returns></returns>
- public Rect TransformRect(Rect rect, DisplayObject targetSpace)
- {
- if (targetSpace == this)
- return rect;
- if (targetSpace == parent && _rotation.z == 0) // optimization
- {
- Vector3 vec = cachedTransform.localScale;
- return new Rect((this.x + rect.x) * vec.x, (this.y + rect.y) * vec.y,
- rect.width * vec.x, rect.height * vec.y);
- }
- else
- {
- Vector4 vec4 = new Vector4(float.MaxValue, float.MaxValue, float.MinValue, float.MinValue);
- TransformRectPoint(rect.xMin, rect.yMin, targetSpace, ref vec4);
- TransformRectPoint(rect.xMax, rect.yMin, targetSpace, ref vec4);
- TransformRectPoint(rect.xMin, rect.yMax, targetSpace, ref vec4);
- TransformRectPoint(rect.xMax, rect.yMax, targetSpace, ref vec4);
- return Rect.MinMaxRect(vec4.x, vec4.y, vec4.z, vec4.w);
- }
- }
- protected void TransformRectPoint(float px, float py, DisplayObject targetSpace, ref Vector4 vec4)
- {
- Vector2 v = TransformPoint(new Vector2(px, py), targetSpace);
- if (vec4.x > v.x) vec4.x = v.x;
- if (vec4.z < v.x) vec4.z = v.x;
- if (vec4.y > v.y) vec4.y = v.y;
- if (vec4.w < v.y) vec4.w = v.y;
- }
- /// <summary>
- ///
- /// </summary>
- public void RemoveFromParent()
- {
- if (parent != null)
- parent.RemoveChild(this);
- }
- /// <summary>
- ///
- /// </summary>
- public void InvalidateBatchingState()
- {
- if (parent != null)
- parent.InvalidateBatchingState(true);
- }
- virtual public void Update(UpdateContext context)
- {
- if (_checkPixelPerfect != 0)
- {
- if (_rotation == Vector3.zero)
- {
- Vector3 v = cachedTransform.localPosition;
- v.x = Mathf.Round(v.x);
- v.y = Mathf.Round(v.y);
- _pixelPerfectAdjustment = v - cachedTransform.localPosition;
- if (_pixelPerfectAdjustment != Vector3.zero)
- cachedTransform.localPosition = v;
- }
- _checkPixelPerfect = 0;
- }
- if (graphics != null)
- graphics.Update(context, context.alpha * _alpha, context.grayed | _grayed);
- if (_paintingMode != 0)
- {
- UpdatePainting();
- //如果是容器,Capture要等到Container.Update的最后执行,因为容器中可能也有需要Capture的内容,要等他们完成后再进行容器的Capture。
- if (!(this is Container))
- {
- if ((_flags & Flags.CacheAsBitmap) == 0 || _paintingInfo.flag != 2)
- UpdateContext.OnEnd += _paintingInfo.captureDelegate;
- }
- paintingGraphics.Update(context, 1, false);
- }
- if (_filter != null)
- _filter.Update();
- Stats.ObjectCount++;
- }
- void UpdatePainting()
- {
- NTexture paintingTexture = paintingGraphics.texture;
- if (paintingTexture != null && paintingTexture.disposed) //Texture可能已被Stage.MonitorTexture销毁
- {
- paintingTexture = null;
- _paintingInfo.flag = 1;
- }
- if (_paintingInfo.flag == 1)
- {
- _paintingInfo.flag = 0;
- //从优化考虑,决定使用绘画模式的容器都需要明确指定大小,而不是自动计算包围。这在UI使用上并没有问题,因为组件总是有固定大小的
- Margin extend = _paintingInfo.extend;
- paintingGraphics.contentRect = new Rect(-extend.left, -extend.top, _contentRect.width + extend.left + extend.right, _contentRect.height + extend.top + extend.bottom);
- int textureWidth = Mathf.RoundToInt(paintingGraphics.contentRect.width * _paintingInfo.scale);
- int textureHeight = Mathf.RoundToInt(paintingGraphics.contentRect.height * _paintingInfo.scale);
- if (paintingTexture == null || paintingTexture.width != textureWidth || paintingTexture.height != textureHeight)
- {
- if (paintingTexture != null)
- paintingTexture.Dispose();
- if (textureWidth > 0 && textureHeight > 0)
- {
- paintingTexture = new NTexture(CaptureCamera.CreateRenderTexture(textureWidth, textureHeight, UIConfig.depthSupportForPaintingMode));
- Stage.inst.MonitorTexture(paintingTexture);
- }
- else
- paintingTexture = null;
- paintingGraphics.texture = paintingTexture;
- }
- }
- if (paintingTexture != null)
- paintingTexture.lastActive = Time.time;
- }
- void Capture()
- {
- if (paintingGraphics.texture == null)
- return;
- Vector2 offset = new Vector2(_paintingInfo.extend.left, _paintingInfo.extend.top);
- CaptureCamera.Capture(this, (RenderTexture)paintingGraphics.texture.nativeTexture, paintingGraphics.contentRect.height, offset);
- _paintingInfo.flag = 2; //2表示已完成一次Capture
- if (onPaint != null)
- onPaint();
- }
- /// <summary>
- /// 为对象设置一个默认的父Transform。当对象不在显示列表里时,它的GameObject挂到哪里。
- /// </summary>
- public Transform home
- {
- get { return _home; }
- set
- {
- _home = value;
- if (value != null && cachedTransform.parent == null)
- cachedTransform.SetParent(value, false);
- }
- }
- void UpdateHierarchy()
- {
- if ((_flags & Flags.GameObjectDisposed) != 0)
- return;
- if ((_flags & Flags.UserGameObject) != 0)
- {
- //we dont change transform parent of this object
- if (gameObject != null)
- {
- if (parent != null && visible)
- gameObject.SetActive(true);
- else
- gameObject.SetActive(false);
- }
- }
- else if (parent != null)
- {
- cachedTransform.SetParent(parent.cachedTransform, false);
- if (_visible)
- gameObject.SetActive(true);
- int layerValue = parent.gameObject.layer;
- if (parent._paintingMode != 0)
- layerValue = CaptureCamera.hiddenLayer;
- SetLayer(layerValue, true);
- }
- else if ((_flags & Flags.Disposed) == 0 && this.gameObject != null && !StageEngine.beingQuit)
- {
- if (Application.isPlaying)
- {
- if (gOwner == null || gOwner.parent == null)//如果gOwner还有parent的话,说明只是暂时的隐藏
- {
- cachedTransform.SetParent(_home, false);
- if (_home == null)
- UnityEngine.Object.DontDestroyOnLoad(this.gameObject);
- }
- }
- gameObject.SetActive(false);
- }
- }
- virtual protected bool SetLayer(int value, bool fromParent)
- {
- if ((_flags & Flags.LayerSet) != 0) //setted
- {
- if (fromParent)
- return false;
- }
- else if ((_flags & Flags.LayerFromParent) != 0) //inherit from parent
- {
- if (!fromParent)
- _flags |= Flags.LayerSet;
- }
- else
- {
- if (fromParent)
- _flags |= Flags.LayerFromParent;
- else
- _flags |= Flags.LayerSet;
- }
- if (_paintingMode > 0)
- paintingGraphics.gameObject.layer = value;
- else if (gameObject.layer != value)
- {
- gameObject.layer = value;
- if ((this is Container))
- {
- int cnt = ((Container)this).numChildren;
- for (int i = 0; i < cnt; i++)
- {
- DisplayObject child = ((Container)this).GetChildAt(i);
- child.SetLayer(value, true);
- }
- }
- }
- return true;
- }
- internal void _SetLayerDirect(int value)
- {
- if (_paintingMode > 0)
- paintingGraphics.gameObject.layer = value;
- else
- gameObject.layer = value;
- }
- virtual public void Dispose()
- {
- if ((_flags & Flags.Disposed) != 0)
- return;
- _flags |= Flags.Disposed;
- RemoveFromParent();
- RemoveEventListeners();
- if (graphics != null)
- graphics.Dispose();
- if (_filter != null)
- _filter.Dispose();
- if (paintingGraphics != null)
- {
- if (paintingGraphics.texture != null)
- paintingGraphics.texture.Dispose();
- paintingGraphics.Dispose();
- if (paintingGraphics.gameObject != this.gameObject)
- {
- if (Application.isPlaying)
- UnityEngine.Object.Destroy(paintingGraphics.gameObject);
- else
- UnityEngine.Object.DestroyImmediate(paintingGraphics.gameObject);
- }
- }
- DestroyGameObject();
- }
- internal void DisplayDisposedWarning()
- {
- if ((_flags & Flags.DisposedWarning) == 0)
- {
- _flags |= Flags.DisposedWarning;
- StringBuilder sb = new StringBuilder();
- sb.Append("DisplayObject is still in use but GameObject was disposed. (");
- if (gOwner != null)
- {
- sb.Append("type=").Append(gOwner.GetType().Name).Append(", x=").Append(gOwner.x).Append(", y=").Append(gOwner.y).Append(", name=").Append(gOwner.name);
- if (gOwner.packageItem != null)
- sb.Append(", res=" + gOwner.packageItem.name);
- }
- else
- {
- sb.Append("id=").Append(id).Append(", type=").Append(this.GetType().Name).Append(", name=").Append(name);
- }
- sb.Append(")");
- Debug.LogError(sb.ToString());
- }
- }
- protected internal class PaintingInfo
- {
- public Action captureDelegate; //缓存这个delegate,可以防止Capture状态下每帧104B的GC
- public Margin extend;
- public float scale;
- public int flag;
- }
- [Flags]
- protected internal enum Flags
- {
- Disposed = 1,
- UserGameObject = 2,
- TouchDisabled = 4,
- OutlineChanged = 8,
- UpdatingSize = 0x10,
- WidthChanged = 0x20,
- HeightChanged = 0x40,
- PixelPerfect = 0x80,
- LayerSet = 0x100,
- LayerFromParent = 0x200,
- NotFocusable = 0x400,
- TabStop = 0x800,
- TabStopChildren = 0x1000,
- FairyBatching = 0x2000,
- BatchingRequested = 0x4000,
- BatchingRoot = 0x8000,
- SkipBatching = 0x10000,
- CacheAsBitmap = 0x20000,
- GameObjectDisposed = 0x40000,
- DisposedWarning = 0x80000
- }
- }
- /// <summary>
- ///
- /// </summary>
- public class DisplayObjectInfo : MonoBehaviour
- {
- /// <summary>
- ///
- /// /// </summary>
- [System.NonSerialized]
- public DisplayObject displayObject;
- private void OnDestroy()
- {
- if (displayObject != null)
- displayObject._flags |= DisplayObject.Flags.GameObjectDisposed;
- }
- //public static string FontName;
- //private void LoadLocalizationFont()
- //{
- // switch (GameEntry.Localization.Language)
- // {
- // case GameFramework.Localization.Language.ChineseSimplified:
- // case GameFramework.Localization.Language.ChineseSimplified:
- // LoadFont2("FZZCH_GBK");
- // break;
- // case GameFramework.Localization.Language.ChineseSimplified:
- // LoadFont2("MyFont");
- // break;
- // default:
- // LoadFont2("IMPACT");
- // break;
- // }
- //}
- //private void LoadFont2(string fontName)
- //{
- // BaseFont newFont = FontManager.GetFont(fontName);
- // if (newFont == null)
- // {
- // Debug.Log("字体:" + fontName + ",不存在Resources或Resources/Fonts文件夹下");
- // return;
- // }
- // FGuiForm.setMainFont(fontName);
- //}
- //public static void SetMainFont(string fontName)
- //{
- // FontName = fontName;
- //}
- //private void SetFont()
- //{
- // DisplayObjectInfo[] displayObjectInfos = GetComponentsInChildren<DisplayObjectInfo>(true);
- // foreach (var item in displayObjectInfos)
- // {
- // if (item.displayObject.GetType() == typeof(TextField))
- // {
- // TextField textField = item.displayObject as TextField;
- // textField.textFormat.font = FontName;
- // textField.ApplyFormat();
- // }
- // else if (item.displayObject.GetType() == typeof(InputTextField))
- // {
- // InputTextField textField = item.displayObject as InputTextField;
- // textField.textFormat.font = FontName;
- // }
- // }
- //}
- }
- }
|