| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- 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<T>() 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<ComponentView>().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);
- }
- }
- }
|