Beispiel #1
0
        //TODO -> previously, we had this in awake to get around an issue where entities present in the scene that never had Awake() called
        //we not properly being disposed of -> OnDestroy + Disposer.Dispose() is not called
        //however, this made setup not so straightforward when instantiating entities from prefabs
        //need to work out a proper solution for both cases
        public override void Initialize(IEventSystem eventSystem)
        {
            base.Initialize(eventSystem);

            if (!Entity.HasComponent <ViewComponent>())
            {
                var viewComponent = new ViewComponent();
                AddTransformToView(viewComponent);
                Entity.AddComponent(viewComponent);
            }
            else
            {
                AddTransformToView(Entity.GetComponent <ViewComponent>());
            }

            foreach (var blueprint in Blueprints)
            {
                blueprint.Apply(this.Entity);
            }

            if (Components != null && Components.Length > 0)
            {
                Entity.AddComponents(Components.Select(c => c as object).Where(o => o != null).ToArray());
            }
            else
            {
                var monoBehaviours = GetComponents <Component>().Where(c => c != null && c.GetType() != typeof(Transform) && c.GetType() != typeof(EntityBehaviour)).ToArray();
                Entity.AddComponents(monoBehaviours);
            }
        }
Beispiel #2
0
        /* TODO
         * this gets us around the "force enable" issue but
         * we still may have a problem of garbage entities
         * because the entity gets added to the pool with or without awake being called
         */
        void Awake()
        {
            if (!Entity.HasComponent <ViewComponent> ())
            {
                var viewComponent = new ViewComponent();
                AddTransformToView(viewComponent);
                Entity.AddComponent(viewComponent);
            }
            else
            {
                AddTransformToView(Entity.GetComponent <ViewComponent> ());
            }

            for (var i = 0; i < ComponentTypes.Count(); i++)
            {
                var type = ComponentTypes[i].GetTypeWithAssembly();
                if (type == null)
                {
                    throw new Exception("Cannot resolve type for [" + ComponentTypes[i] + "]");
                }

                var component = (object)Activator.CreateInstance(type);
                JsonUtility.FromJsonOverwrite(ComponentData[i], component);
                Entity.AddComponent(component);
            }


            //for (var i = 0; i < Components.Count; i++)
            //{
            //             var component = Instantiate(Components[i]);
            //	Entity.AddComponent(component);
            //}

            foreach (var blueprint in Blueprints)
            {
                blueprint.Apply(this.Entity);
            }

            var monoBehaviours = GetComponents <Component>();

            foreach (var mb in monoBehaviours)
            {
                if (mb == null)
                {
                    Debug.LogWarning("Component on " + this.gameObject.name + " is null!");
                }
                else
                {
                    if (mb.GetType() != typeof(Transform) && mb.GetType() != typeof(EntityBehaviour))
                    {
                        Entity.AddComponent(mb);
                    }
                }
            }
        }
Beispiel #3
0
 private void AddTransformToView(ViewComponent viewComponent)
 {
     //ensure that the root EntityBehaviour's transform is first
     if (Proxy == null)
     {
         viewComponent.Transforms.Insert(0, this.transform);
     }
     else
     {
         viewComponent.Transforms.Add(this.transform);
     }
 }
        public override void Setup(IEventSystem eventSystem)
        {
            base.Setup(eventSystem);

            for (var i = 0; i < CachedComponents.Count(); i++)
            {
                var typeName = CachedComponents[i];
                var type     = TypeUtilities.GetTypeWithAssembly(typeName);
                if (type == null)
                {
                    throw new Exception("Cannot resolve type for [" + typeName + "]");
                }

                var component           = (object)Activator.CreateInstance(type);
                var componentProperties = JSON.Parse(CachedProperties[i]);
                component.Deserialize(componentProperties);

                Entity.AddComponent(component);
            }

            if (!Entity.HasComponent <ViewComponent> ())
            {
                var viewComponent = new ViewComponent();
                viewComponent.Transforms.Add(this.transform);
                Entity.AddComponent(viewComponent);
            }
            else
            {
                var viewComponent = Entity.GetComponent <ViewComponent> ();
                viewComponent.Transforms.Add(this.transform);
            }

            var monoBehaviours = GetComponents <Component>();

            foreach (var mb in monoBehaviours)
            {
                if (mb == null)
                {
                    Debug.LogWarning("Component on " + this.gameObject.name + " is null!");
                }
                else
                {
                    if (mb.GetType() != typeof(Transform) && mb.GetType() != typeof(EntityBehaviour))
                    {
                        Entity.AddComponent(mb);
                    }
                }
            }
        }