using Sirenix.OdinInspector; using System; using System.Collections; using System.Collections.Generic; #if UNITY_EDITOR using UnityEditor; #endif using UnityEngine; namespace Ether { [DisallowMultipleComponent] public class Entity : SerializedMonoBehaviour { [Header("实例配置")] [LabelText("唯一Key值")] [PropertyOrder(-10)] public string key; [LabelText("不销毁对象")] [PropertyOrder(-9)] public bool dontDestroy; private void Awake() { EntityManager.Inst.AddEntity(this); if (dontDestroy) { GameObject.DontDestroyOnLoad(this); } } protected virtual void OnClear() { } private void OnDestroy() { EntityManager.Inst.RemoveEntity(this); OnClear(); } void OnApplicationQuit() { DestroyImmediate(gameObject); } void OnValidate() { if (string.IsNullOrEmpty(key)) { GenerateKey(); } } [PropertySpace(5, 20)] [ButtonGroup("ButtonGroup")] [Button("生成唯一Key", ButtonSizes.Medium)] [PropertyOrder(-8)] public void GenerateKey() { // 获取当前时间戳并转换为int类型 //System.DateTime startTime = new System.DateTime(1970, 1, 1); //System.TimeSpan timeSinceStartTime = System.DateTime.Now - startTime; //id = (long)timeSinceStartTime.TotalMilliseconds; #if UNITY_EDITOR key = GUID.Generate().ToString(); #endif } } }