コード例 #1
0
        protected override void OnUpdateBinding(UTinyEntity entity, UTinyObject component)
        {
            var transform = entity.View.transform;

            transform.localPosition = component.GetProperty <Vector3>("localPosition");
            transform.localRotation = component.GetProperty <Quaternion>("localRotation");
            transform.localScale    = component.GetProperty <Vector3>("localScale");
        }
        protected override void OnUpdateBinding(UTinyEntity entity, UTinyObject component)
        {
            OnAddBinding(entity, component);

            var behaviour = GetComponent <RectHitBox2D>(entity);

            behaviour.Box = component.GetProperty <Rect>("box");
        }
コード例 #3
0
        protected override void OnRemoveBinding(UTinyEntity entity, UTinyObject component)
        {
            var camera = GetComponent <Camera>(entity);

            if (camera && camera != null)
            {
                camera.transparencySortMode = TransparencySortMode.Default;
            }
        }
コード例 #4
0
            protected override void OnRemoveComponent(UTinyEntity entity, UTinyObject component)
            {
                var module = GetComponent <ParticleSystem>(entity).rotationOverLifetime;

                // This should be false, but for some reason, the particle system continues to rotate.
                module.enabled      = true;
                module.separateAxes = true;
                module.x            = module.y = module.z = NoCurve;
            }
コード例 #5
0
        protected override void OnAddBinding(UTinyEntity entity, UTinyObject component)
        {
            var renderer = GetComponent <SpriteRenderer>(entity);

            renderer.drawMode = Translate(component.GetProperty <TileMode>("mode"));
            if (component.GetProperty <Vector2>("size") == Vector2.zero)
            {
                component.AssignPropertyFrom("size", renderer.size);
            }
        }
コード例 #6
0
        protected void RemoveComponent <TComponent>(UTinyEntity entity)
            where TComponent : Component
        {
            var component = GetComponent <TComponent>(entity);

            if (null != component)
            {
                Object.DestroyImmediate(component, false);
            }
        }
コード例 #7
0
        protected override void OnUpdateBinding(UTinyEntity entity, UTinyObject component)
        {
            var camera = GetComponent <Camera>(entity);

            if (camera && camera != null)
            {
                camera.transparencySortMode = TransparencySortMode.CustomAxis;
                camera.transparencySortAxis = component.GetProperty <Vector3>("axis");
            }
        }
コード例 #8
0
        protected override void OnUpdateBinding(UTinyEntity entity, UTinyObject textRenderer)
        {
            OnAddBinding(entity, textRenderer);
#if UNITY_EDITOR
            try
            {
                var textMesh = GetComponent <TextMesh>(entity);

                textMesh.text     = textRenderer.GetProperty <string>("text");
                textMesh.fontSize = textRenderer.GetProperty <int>("fontSize");

                var bold   = textRenderer.GetProperty <bool>("bold");
                var italic = textRenderer.GetProperty <bool>("italic");

                if (bold && italic)
                {
                    textMesh.fontStyle = FontStyle.BoldAndItalic;
                }
                else if (bold)
                {
                    textMesh.fontStyle = FontStyle.Bold;
                }
                else if (italic)
                {
                    textMesh.fontStyle = FontStyle.Italic;
                }
                else
                {
                    textMesh.fontStyle = FontStyle.Normal;
                }

                textMesh.characterSize = 10;
                textMesh.lineSpacing   = 1;
                textMesh.richText      = false;
                textMesh.alignment     = TextAlignment.Left;
                textMesh.anchor        = textRenderer.GetProperty <TextAnchor>("anchor");
                textMesh.color         = textRenderer.GetProperty <Color>("color");
                textMesh.font          = textRenderer.GetProperty <Font>("font");

                if (textMesh.font)
                {
                    var renderer = GetComponent <MeshRenderer>(entity);
                    var block    = new MaterialPropertyBlock();
                    renderer.GetPropertyBlock(block);
                    block.Clear();
                    block.SetTexture("_MainTex", textMesh.font.material.mainTexture);
                    renderer.SetPropertyBlock(block);
                }
            }
            finally
            {
                UnityEditor.SceneView.RepaintAll();
            }
#endif
        }
        protected override void OnUpdateBinding(UTinyEntity entity, UTinyObject component)
        {
            OnAddBinding(entity, component);
            var collider = GetComponent <BoxCollider2D>(entity);
            var pivot    = component.GetProperty <Vector2>("pivot");
            var width    = component.GetProperty <float>("width");
            var height   = component.GetProperty <float>("height");

            collider.size   = new Vector2(width, height);
            collider.offset = new Vector2(-(pivot.x - 0.5f) * width, -(pivot.y - 0.5f) * height);
        }
 protected override void OnAddBinding(UTinyEntity entity, UTinyObject component)
 {
     AddMissingComponent <ParticleSystem>(entity, (system) =>
     {
         var renderer      = system.GetComponent <ParticleSystemRenderer>();
         renderer.material = new Material(Shader.Find("UTiny/Particle2D"));
         renderer.mesh     = GenerateQuad();
         var emission      = GetComponent <ParticleSystem>(entity).emission;
         emission.enabled  = null != entity.GetComponent(entity.Registry.GetEmitterBoxSourceType());
     });
 }
コード例 #11
0
 private bool MatchesRequiredComponentTypes(UTinyEntity entity)
 {
     foreach (var type in m_RequiredComponentTypes)
     {
         if (!Has(entity, type))
         {
             return(false);
         }
     }
     return(true);
 }
        protected override void OnUpdateBinding(UTinyEntity entity, UTinyObject component)
        {
            OnAddBinding(entity, component);

            var spriteRenderer = entity.GetComponent(entity.Registry.GetSprite2DRendererType());
            var sprite         = spriteRenderer?.GetProperty <Sprite>("sprite");

            var behaviour = GetComponent <Sprite2DRendererHitBox2D>(entity);

            behaviour.Sprite = sprite;
        }
コード例 #13
0
        protected TComponent AddMissingComponent <TComponent>(UTinyEntity entity, Action <TComponent> init)
            where TComponent : Component
        {
            var component = GetComponent <TComponent>(entity);

            if (!component && null == component)
            {
                component = AddComponent(entity, init);
            }
            return(component);
        }
コード例 #14
0
        private UTinyEntityView CreateLink(UTinyEntity entity)
        {
            try
            {
                var entityRef = (UTinyEntity.Reference)entity;
                if (null != entity.View && entity.View)
                {
                    entity.View.EntityRef = entityRef;
                    entity.View.Registry  = entity.Registry;
                    entity.View.gameObject.SetActive(entity.Enabled);
                    entity.View.gameObject.layer = entity.Layer;
                    return(null);
                }

                // We may have recreated the entity, try to find an active view
                {
                    var scene = UTinyEditorApplication.EntityGroupManager.UnityScratchPad;
                    foreach (var r in scene.GetRootGameObjects())
                    {
                        foreach (var v in r.GetComponentsInChildren <UTinyEntityView>())
                        {
                            if (v.EntityRef.Equals(entityRef))
                            {
                                entity.View           = v;
                                entity.View.EntityRef = entityRef;
                                entity.View.Registry  = entity.Registry;
                                v.gameObject.SetActive(entity.Enabled);
                                v.gameObject.layer = entity.Layer;
                                return(null);
                            }
                        }
                    }
                }

                var go   = new GameObject(entity.Name);
                var view = go.AddComponent <UTinyEntityView>();
                view.gameObject.SetActive(entity.Enabled);
                view.gameObject.layer = entity.Layer;
                view.EntityRef        = entityRef;
                view.Registry         = entity.Registry;
                entity.View           = view;

                return(view);
            }
            finally
            {
                // At this point, it is not clear if the bindings have been added or not (we may have undo-ed something).
                entity.OnComponentAdded   -= HandleComponentAdded;
                entity.OnComponentRemoved -= HandleComponentRemoved;
                entity.OnComponentAdded   += HandleComponentAdded;
                entity.OnComponentRemoved += HandleComponentRemoved;
                BindingsHelper.RunAllBindings(entity);
            }
        }
コード例 #15
0
        public static void RunAllBindings(UTinyEntity entity)
        {
            if (!ComponentBinding.ValidateBindingsParams(entity))
            {
                return;
            }

            foreach (var component in entity.Components)
            {
                RunBindings(entity, component);
            }
        }
        protected override void OnAddComponent(UTinyEntity entity, UTinyObject component)
        {
            var spriteRenderer = entity.GetComponent(entity.Registry.GetSprite2DRendererType());
            var sprite         = spriteRenderer?.GetProperty <Sprite>("sprite");

            if (null == sprite || !sprite)
            {
                return;
            }

            component.AssignPropertyFrom("width", sprite.bounds.size.x);
            component.AssignPropertyFrom("height", sprite.bounds.size.y);
        }
コード例 #17
0
        private void DeleteLink(UTinyEntity entity)
        {
            entity.OnComponentAdded   -= HandleComponentAdded;
            entity.OnComponentRemoved -= HandleComponentRemoved;

            var view = entity.View;

            if (null != view && view && entity.View.gameObject)
            {
                Object.DestroyImmediate(entity.View.gameObject, false);
            }

            entity.View = null;
        }
コード例 #18
0
        public UTinyEntity CreateEntity(UTinyId id, string name)
        {
            var entity = new UTinyEntity(this, m_VersionStorage)
            {
                Id   = id,
                Name = name
            };

            m_VersionStorage.MarkAsChanged(entity);

            Register(entity);

            return(entity);
        }
コード例 #19
0
        protected TComponent AddComponent <TComponent>(UTinyEntity entity, Action <TComponent> init)
            where TComponent : Component
        {
            if (!ValidateBindingsParams(entity))
            {
                return(null);
            }

            var component = entity.View.gameObject.AddComponent <TComponent>();

            if (null != component)
            {
                init?.Invoke(component);
            }
            return(component);
        }
コード例 #20
0
        private void HandleComponentRemoved(UTinyEntity entity, UTinyObject component)
        {
            component.Refresh();
            var type = component.Type.Dereference(Registry);

            if (type.HasAttribute <BindingsAttribute>())
            {
                var bindings = type.GetAttribute <BindingsAttribute>().Binding;

                // Invoke callback to perform teardown hook
                bindings.Run(BindingTiming.OnRemoveComponent, entity, component);

                // Invoke binding to unregister and remove `unity` components
                bindings.Run(BindingTiming.OnRemoveBindings, entity, component);
            }
        }
コード例 #21
0
        private void HandleComponentAdded(UTinyEntity entity, UTinyObject component)
        {
            component.Refresh();
            var type = component.Type.Dereference(Registry);

            if (type.HasAttribute <BindingsAttribute>())
            {
                var bindings = type.GetAttribute <BindingsAttribute>().Binding;

                // Invoke bindings to register and add `unity` components
                bindings.Run(BindingTiming.OnAddBindings, entity, component);
                bindings.Run(BindingTiming.OnUpdateBindings, entity, component);

                // Invoke callback to perform first time setup hook
                bindings.Run(BindingTiming.OnAddComponent, entity, component);
            }
        }
コード例 #22
0
        protected override void OnUpdateBinding(UTinyEntity entity, UTinyObject sprite2DRenderer)
        {
#if UNITY_EDITOR
            try
            {
                OnAddBinding(entity, sprite2DRenderer);
                var sprite         = sprite2DRenderer.GetProperty <Sprite>("sprite");
                var spriteRenderer = GetComponent <SpriteRenderer>(entity);

                var block = new MaterialPropertyBlock();
                spriteRenderer.GetPropertyBlock(block);
                block.Clear();

                SetColorProperty(sprite2DRenderer, block);

                if (sprite)
                {
                    spriteRenderer.sprite = sprite;
                    var     blending = sprite2DRenderer.GetProperty <UTinyEnum.Reference>("blending").Value;
                    Vector2 blendMode;
                    if (s_BlendModes.TryGetValue(blending, out blendMode))
                    {
                        spriteRenderer.sharedMaterial.SetFloat("_SrcMode", blendMode.x);
                        spriteRenderer.sharedMaterial.SetFloat("_DstMode", blendMode.y);
                    }
                    else
                    {
                        Debug.Log($"{UTinyConstants.ApplicationName}: Unknown blending mode, of value '{blending}'");
                    }

                    block.SetTexture("_MainTex", sprite.texture);
                    SetColorProperty(sprite2DRenderer, block);
                }
                else
                {
                    spriteRenderer.sprite = null;
                }

                spriteRenderer.SetPropertyBlock(block);
            }
            finally
            {
                UnityEditor.SceneView.RepaintAll();
            }
#endif
        }
        protected override void OnAddComponent(UTinyEntity entity, UTinyObject component)
        {
            var spriteRenderer = entity.GetComponent(entity.Registry.GetSprite2DRendererType());
            var sprite         = spriteRenderer?.GetProperty <Sprite>("sprite");

            if (null == sprite || !sprite)
            {
                return;
            }

            var rect = new Rect
            {
                min = sprite.bounds.min,
                max = sprite.bounds.max
            };

            component.AssignPropertyFrom("box", rect);
        }
            protected override void OnUpdateBinding(UTinyEntity entity, UTinyObject component)
            {
                var module         = GetComponent <ParticleSystem>(entity).colorOverLifetime;
                var registry       = entity.Registry;
                var gradientRef    = component.GetProperty <UTinyEntity.Reference>("gradient");
                var gradientEntity = gradientRef.Dereference(registry);
                var colorGradient  = gradientEntity?.GetComponent(registry.GetGradientType());

                if (null == gradientEntity || null == colorGradient)
                {
                    module.enabled = false;
                }
                else
                {
                    module.enabled = true;
                    module.color   = new ParticleSystem.MinMaxGradient(colorGradient.As <Gradient>());
                }
            }
コード例 #25
0
        public static void RunBindings(UTinyEntity entity, UTinyObject component)
        {
            if (!ComponentBinding.ValidateBindingsParams(entity, component))
            {
                return;
            }

            var type = component.Type.Dereference(entity.Registry);

            if (!type.HasAttribute <BindingsAttribute>())
            {
                return;
            }

            var bindings = type.GetAttribute <BindingsAttribute>().Binding;

            bindings.Run(BindingTiming.OnAddBindings, entity, component);
            bindings.Run(BindingTiming.OnUpdateBindings, entity, component);
        }
コード例 #26
0
        protected override void OnRemoveComponent(UTinyEntity entity, UTinyObject component)
        {
            // We need to set the parent of the associated Unity Object to null.
            // And potentially move them outside of view.

            entity.SetParent(UTinyEntity.Reference.None);
            var children = entity.EntityGroup.Entities
                           .Deref(entity.Registry)
                           .Where(e =>
            {
                var t = e.GetComponent(entity.Registry.GetTransformType());
                return(null != t && (t.GetProperty <UTinyEntity.Reference>("parent")).Equals((UTinyEntity.Reference)entity));
            });

            foreach (var child in children)
            {
                child.SetParent(UTinyEntity.Reference.None);
            }
        }
コード例 #27
0
            protected override void OnUpdateBinding(UTinyEntity entity, UTinyObject component)
            {
                var module      = GetComponent <ParticleSystem>(entity).sizeOverLifetime;
                var registry    = entity.Registry;
                var curveRef    = component.GetProperty <UTinyEntity.Reference>("curve");
                var curveEntity = curveRef.Dereference(registry);
                var curve       = curveEntity?.GetComponent(registry.GetCurveType());

                if (null == curveEntity || null == curve)
                {
                    module.enabled = false;
                }
                else
                {
                    module.enabled      = true;
                    module.separateAxes = false;
                    module.size         = new ParticleSystem.MinMaxCurve(1.0f, curve.As <AnimationCurve>());
                }
            }
        private void SetSortingLayerOrder(UTinyEntity entity, int sortingLayerID, int sortingOrder)
        {
            var group = GetComponent <SortingGroup>(entity);

            if (group && group != null)
            {
                group.sortingLayerID = sortingLayerID;
                group.sortingOrder   = sortingOrder;
                EditorUtility.SetDirty(group);
            }

            var renderer = GetComponent <SpriteRenderer>(entity);

            if (renderer && renderer != null)
            {
                renderer.sortingLayerID = sortingLayerID;
                renderer.sortingOrder   = sortingOrder;
                EditorUtility.SetDirty(renderer);
            }
        }
        public static void SetEntity(UTinyEntity entity, bool additive = false)
        {
            var instance = GetInstance();

            instance.Registry  = entity.Registry;
            instance.EntityRef = (UTinyEntity.Reference)entity;

            if (!additive)
            {
                Selection.activeInstanceID = instance.GetInstanceID();
            }
            else
            {
                if (!Selection.instanceIDs.Contains(instance.GetInstanceID()))
                {
                    var selection = Selection.instanceIDs.ToList();
                    selection.Add(instance.GetInstanceID());
                    Selection.instanceIDs = selection.ToArray();
                }
            }
        }
            protected override void OnUpdateBinding(UTinyEntity entity, UTinyObject component)
            {
                var module      = GetComponent <ParticleSystem>(entity).colorOverLifetime;
                var registry    = entity.Registry;
                var curveRef    = component.GetProperty <UTinyEntity.Reference>("curve");
                var curveEntity = curveRef.Dereference(registry);
                var curve       = curveEntity?.GetComponent(registry.GetCurveType());

                if (UsesLifetimeColor(entity))
                {
                    return;
                }

                if (null == curveEntity || null == curve)
                {
                    module.enabled = false;
                }
                else
                {
                    module.enabled = true;
                    module.color   = new ParticleSystem.MinMaxGradient(ConvertToAlphaGradient(curve));
                }
            }