using System; using ETModel; using MongoDB.Bson.Serialization.Attributes; #if UNITY_EDITOR using UnityEngine; #endif namespace ETHotfix { [BsonIgnoreExtraElements] public abstract class Component : Object, IDisposable { [BsonIgnore] public long InstanceId { get; protected set; } #if !SERVER [BsonIgnore] public GameObject GameObject { get; protected set; } #endif [BsonIgnore] private bool isFromPool; [BsonIgnore] public bool IsFromPool { get { return this.isFromPool; } set { this.isFromPool = value; if (!this.isFromPool) { return; } if (this.InstanceId == 0) { this.InstanceId = IdGenerater.GenerateId(); } } } [BsonIgnore] public bool IsDisposed { get { return this.InstanceId == 0; } } private Component parent; [BsonIgnore] public Component Parent { get { return this.parent; } set { this.parent = value; #if !SERVER if (this.parent == null) { this.GameObject.transform.SetParent(GameObject.Find("/Global").transform, false); return; } if (this.GameObject != null) { if (this.parent.GameObject != null) { this.GameObject.transform.SetParent(this.parent.GameObject.transform, false); } } #endif } } public T GetParent() where T : Component { return this.Parent as T; } [BsonIgnore] public Entity Entity { get { return this.Parent as Entity; } } protected Component() { this.InstanceId = IdGenerater.GenerateId(); #if !SERVER if (!this.GetType().IsDefined(typeof(HideInHierarchy), true)) { this.GameObject = new GameObject(); this.GameObject.name = this.GetType().Name; this.GameObject.layer = LayerNames.GetLayerInt(LayerNames.HIDDEN); this.GameObject.AddComponent().Component = this; } #endif } public virtual void Dispose() { if (this.IsDisposed) { return; } // 触发Destroy事件 Game.EventSystem.Destroy(this); Game.EventSystem.Remove(this.InstanceId); this.InstanceId = 0; if (this.IsFromPool) { Game.ObjectPool.Recycle(this); } else { #if !SERVER UnityEngine.Object.Destroy(this.GameObject); #endif } } public override void EndInit() { Game.EventSystem.Deserialize(this); } public override string ToString() { return MongoHelper.ToJson(this); } } }