示例#1
0
        public T GetComponent <T>() where T : IComponent, new()
        {
            foreach (IComponent component in Components)
            {
                if (component is T t)
                {
                    return(t);
                }
            }

            T newT = new T();

            newT.DeltinScript = this;

            for (int i = InitComponent.Count - 1; i >= 0; i--)
            {
                if (typeof(T) == InitComponent[i].ComponentType)
                {
                    InitComponent[i].Apply(newT);
                    InitComponent.RemoveAt(i);
                }
            }

            Components.Add(newT);
            newT.Init();

            return(newT);
        }
示例#2
0
 public void ExecOnComponent <T>(Action <T> apply) where T : IComponent
 {
     if (IsComponent <T>(out T existing))
     {
         apply.Invoke(existing);
     }
     else
     {
         InitComponent.Add(new InitComponent(typeof(T), component => apply.Invoke((T)component)));
     }
 }
        private T AddComponent <T>() where T : IComponent, new()
        {
            T newT = new T();

            for (int i = InitComponent.Count - 1; i >= 0; i--)
            {
                if (typeof(T) == InitComponent[i].ComponentType)
                {
                    InitComponent[i].Apply(newT);
                    InitComponent.RemoveAt(i);
                }
            }

            Components.Add(newT);
            newT.Init(this);

            return(newT);
        }