using System.Collections.Generic; using TEngine.Runtime; using TEngine.Runtime.Entity; using UnityEngine; namespace TEngine.Demo.TEngine.EntityDemo { /// /// an enumeration of the types of light /// public enum LightType { DirectionalLight = 0, PointLight = 1, SpotLight = 2, } /// /// 光源实体管理器 /// public class LightEntityMgr : UnitySingleton { public Dictionary m_dictionary = new Dictionary(); public Dictionary TypeDic = new Dictionary(); /// /// OnLoad /// protected override void OnLoad() { TypeDic.Add(LightType.DirectionalLight, "Light/DirectionalLight"); TypeDic.Add(LightType.PointLight, "Light/PointLight"); TypeDic.Add(LightType.SpotLight, "Light/SpotLight"); RegisterEvent(); base.OnLoad(); } /// /// RegisterEvent /// void RegisterEvent() { GameEventMgr.Instance.AddEventListener(EntityEvent.ShowEntitySuccess, OnShowEntitySuccess); GameEventMgr.Instance.AddEventListener(EntityEvent.ShowEntityFailure, OnShowEntityFailure); GameEventMgr.Instance.AddEventListener(EntityEvent.HideEntityComplete, OnHideEntityComplete); } /// /// OnShowEntitySuccess /// /// /// /// private void OnShowEntitySuccess(IEntity entity, float duration, object userData) { Log.Warning("OnShowEntitySuccess" + entity.ToString() + " " + duration + " " + userData); if (!TypeDic.ContainsValue(entity.EntityAssetName)) { return; } if (!m_dictionary.ContainsKey(entity.Id)) { m_dictionary.Add(entity.Id, entity); } } /// /// OnShowEntityFailure /// /// /// /// /// /// private void OnShowEntityFailure(int entityId, string entityAssetName, string entityGroupName, string errorMessage, object userData) { } /// /// OnHideEntityComplete /// /// /// /// /// private void OnHideEntityComplete(int entityId, string entityAssetName, IEntityGroup entityGroup, object userData) { if (!TypeDic.ContainsValue(entityAssetName)) { return; } if (m_dictionary.ContainsKey(entityId)) { m_dictionary.Remove(entityId); } } /// /// Light实体管理器创建光源/ URP可最佳实践 /// /// /// /// public void CreateLight(LightType lightType, Vector3 position,Quaternion quaternion) { if (TypeDic.TryGetValue(lightType, out var lightPath)) { EntityData data = EntityData.Create(position,quaternion); EntitySystem.Instance.CreateEntity(lightPath, data); } } } }