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 { [LabelText("唯一Key值")] public string key; [LabelText("不销毁对象")] public bool dontDestroy; private void Awake() { EntityManager.Inst.AddEntity(this); if (dontDestroy) { GameObject.DontDestroyOnLoad(this); } } private void OnDestroy() { EntityManager.Inst.RemoveEntity(this); } void OnApplicationQuit() { DestroyImmediate(gameObject); } void OnValidate() { if (string.IsNullOrEmpty(key)) { GenerateKey(); } } [PropertySpace(5, 5)] [ButtonGroup("ButtonGroup")] [Button("生成唯一Key", ButtonSizes.Medium)] 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 } } }