private static SpriteMaskTrueSoft getSpriteMask(Component t)
    {
#if BEFORE_4_6
        return(t.GetComponent <SpriteMask> ());
#else
        t.GetComponents <SpriteMaskTrueSoft> (maskComponents);
        SpriteMaskTrueSoft sm = maskComponents.Count == 1 ? maskComponents [0] : null;
        maskComponents.Clear();
        return(sm);
#endif
    }
    /// <summary>
    /// Find mask component for <code>t</code> and call <code>updateSprites(t)</code>.
    /// </summary>
    public static SpriteMaskTrueSoft updateFor(Transform t)
    {
        SpriteMaskTrueSoft mask = getParentMask(t);

        if (mask != null)
        {
            mask.updateSprites(t);
        }

        return(mask);
    }
Example #3
0
    private void createBiteObject(Vector2 pos)
    {
        // Create bite object which will take pare in inverted masking
        GameObject go = new GameObject("Bite");

        go.transform.parent   = transform;
        go.transform.position = pos;
        go.AddComponent <SpriteMaskingPart>();

        SpriteRenderer sr = go.AddComponent <SpriteRenderer> ();

        sr.sprite       = circle;
        sr.sortingOrder = 1;

        // Find SpriteMask in parents od go.transform and update it
        SpriteMaskTrueSoft.updateFor(go.transform);
    }
    void OnDisable()
    {
#if SPRITE_MASK_DEBUG
        log("OnDisable >>>");
#endif
        // Clear stencil ID
        if (_stencilId > 0)
        {
            releaseId(_stencilId);
            _stencilId = 0;
        }

        if (gameObject.activeInHierarchy)
        {
            // Just disabled

            Renderer r = getRenderer(this);
            if (r != null)
            {
                // Turn off renderer
                r.enabled = false;
            }

            // If there is enabled parent mask, then all childs sprites should use its stencil ID
            SpriteMaskTrueSoft parentMask = getParentMask(transform);
            if (parentMask != null)
            {
                parentStencilId = parentMask.stencilId;
            }
            else
            {
                parentStencilId = null;
            }

            updateSprites();
            updateMaskingComponents();
        }

#if SPRITE_MASK_DEBUG
        log("OnDisable <<<");
#endif
    }
Example #5
0
    void Start()
    {
        if (target == null)
        {
            target = gameObject;
        }

        SpriteMaskTrueSoft mask = target.AddComponent <SpriteMaskTrueSoft> ();

        mask.type = SpriteMaskTrueSoft.Type.Rectangle;
        mask.size = new Vector2(350f, 350f);

        attachSprite(spriteCloud, new Vector2(110f, 160f)).sortingOrder     = 1;
        attachSprite(spriteTree, new Vector2(-120f, 75f)).sortingOrder      = 2;
        attachSprite(spriteAnimal1, new Vector2(100f, -40f)).sortingOrder   = 3;
        attachSprite(spriteAnimal2, new Vector2(-160f, -100f)).sortingOrder = 4;

        // Call this to get sprites masked
        mask.updateSprites();
    }
    private void createBloodObject(Vector2 pos)
    {
//		print (Time.frameCount +" createBloodObject(): " + pos);

        GameObject go = new GameObject("Blood");

        go.transform.parent   = transform;
        go.transform.position = pos;

        SpriteRenderer sr = go.AddComponent <SpriteRenderer> ();

        sr.sprite       = blood;
        sr.sortingOrder = 100;

        SpriteMaskExample07DestroyBlood db = go.AddComponent <SpriteMaskExample07DestroyBlood> ();

        db.sr = sr;

        SpriteMaskTrueSoft.updateFor(go.transform);
    }
Example #7
0
    public override void OnInspectorGUI()
    {
        SpriteMaskTrueSoft mask = (SpriteMaskTrueSoft)target;

        bool isPrefab = PrefabUtility.GetPrefabType(mask) == PrefabType.Prefab;

        if (isPrefab)
        {
            EditorGUILayout.HelpBox("Prefab edit unavailable", MessageType.Info);
            return;
        }

        if (Event.current.type == EventType.ValidateCommand && Event.current.commandName == "UndoRedoPerformed")
        {
            // Apply undo
            mask.SendMessage("RequestTypeApply", SendMessageOptions.DontRequireReceiver);
        }

        GUILayoutOption[] options = new GUILayoutOption[0];

        serializedObject.Update();

        SpriteMaskTrueSoft.Type currentType = mask.type;
        SpriteMaskTrueSoft.Type newType     = (SpriteMaskTrueSoft.Type)EditorGUILayout.EnumPopup("Type", currentType, options);

        if (GUI.changed)
        {
            if (currentType != newType)
            {
                Undo.RecordObject(target, "Type change");
                mask.type = newType;
                mask.SendMessage("RequestTypeApply", SendMessageOptions.DontRequireReceiver);
                EditorUtility.SetDirty(target);
            }
        }

        EditorGUILayout.Space();

        bool currentInverted = mask.inverted;
        bool newInverted     = EditorGUILayout.Toggle("Inverted", currentInverted, options);

        if (newInverted)
        {
            EditorGUILayout.HelpBox("'Inverted' is an experimental feature. It may not work properly in some multi level use cases.", MessageType.Info);
        }
        if (GUI.changed)
        {
            if (currentInverted != newInverted)
            {
                Undo.RecordObject(target, "Inverted change");
                mask.inverted = newInverted;
                EditorUtility.SetDirty(target);
            }
        }

        bool currentShowMaskGraphics = mask.showMaskGraphics;
        bool newShowMaskGraphics     = EditorGUILayout.Toggle("Show Mask Graphics", currentShowMaskGraphics, options);

        if (GUI.changed)
        {
            if (currentShowMaskGraphics != newShowMaskGraphics)
            {
                Undo.RecordObject(target, "Show Mask Graphics change");
                mask.showMaskGraphics = newShowMaskGraphics;
                EditorUtility.SetDirty(target);
            }
        }

        if (newType != SpriteMaskTrueSoft.Type.None)
        {
            EditorGUILayout.Space();

            Vector2 currentSize = mask.size;
            Vector2 newSize     = currentSize;

            Vector2 currentPivot = mask.pivot;
            Vector2 newPivot     = currentPivot;

            Sprite currentSprite = mask.sprite;
            Sprite newSprite     = currentSprite;

            Texture2D currentTexture = mask.texture;
            Texture2D newTexture     = currentTexture;

            switch (newType)
            {
            case SpriteMaskTrueSoft.Type.Sprite:
                newSprite = EditorGUILayout.ObjectField("Sprite", currentSprite, typeof(Sprite), true, options) as Sprite;
                break;

            case SpriteMaskTrueSoft.Type.CustomMesh:
                newTexture = EditorGUILayout.ObjectField("Texture", currentTexture, typeof(Texture2D), true, options) as Texture2D;
                break;

            case SpriteMaskTrueSoft.Type.Rectangle:
            case SpriteMaskTrueSoft.Type.Texture:
                if (newType == SpriteMaskTrueSoft.Type.Texture)
                {
                    newTexture = EditorGUILayout.ObjectField("Texture", currentTexture, typeof(Texture2D), true, options) as Texture2D;
                }
                newSize  = EditorGUILayout.Vector2Field("Size", currentSize, options);
                newPivot = EditorGUILayout.Vector2Field("Pivot", currentPivot, options);
                break;
            }



            if (GUI.changed)
            {
                if (currentSize != newSize)
                {
                    Undo.RecordObject(target, "Size change");
                    mask.size = newSize;
                    EditorUtility.SetDirty(target);
                }

                if (currentPivot != newPivot)
                {
                    Undo.RecordObject(target, "Pivot change");
                    mask.pivot = newPivot;
                    EditorUtility.SetDirty(target);
                }

                if (currentSprite != newSprite)
                {
                    Undo.RecordObject(target, "Sprite change");
                    mask.sprite = newSprite;
                    EditorUtility.SetDirty(target);
                }

                if (currentTexture != newTexture)
                {
                    Undo.RecordObject(target, "Texture change");
                    mask.texture = newTexture;
                    EditorUtility.SetDirty(target);
                }
            }

            if (newType == SpriteMaskTrueSoft.Type.Rectangle || newType == SpriteMaskTrueSoft.Type.Texture || newType == SpriteMaskTrueSoft.Type.CustomMesh)
            {
                EditorGUILayout.Space();

                Renderer r = mask.GetComponent <Renderer> ();
                if (r != null)
                {
                    maybeGetSortingParams();

                    int    selectedIdx = -1;
                    string name        = r.sortingLayerName;

                    for (int i = 0; i < sortingLayerNames.Length; i++)
                    {
                        if (name.Equals(sortingLayerNames [i]))
                        {
                            selectedIdx = i;
                        }
                    }

                    if (selectedIdx == -1)
                    {
                        for (int i = 0; i < sortingLayerUniqueIDs.Length; i++)
                        {
                            if (sortingLayerUniqueIDs [i] == 0)
                            {
                                selectedIdx = i;
                            }
                        }
                    }

                    int sortingLayerIdx = EditorGUILayout.Popup("Sorting Layer", selectedIdx, sortingLayerNames);
                    if (selectedIdx != sortingLayerIdx)
                    {
                        Undo.RecordObject(r, "Sorting Layer change");
                        r.sortingLayerName = sortingLayerNames [sortingLayerIdx];
                        EditorUtility.SetDirty(r);
                    }

                    int sortingOrder = EditorGUILayout.IntField("Order in Layer", r.sortingOrder, options);
                    if (sortingOrder != r.sortingOrder)
                    {
                        Undo.RecordObject(r, "Order in Layer");
                        r.sortingOrder = sortingOrder;
                        EditorUtility.SetDirty(r);
                    }
                }
            }
        }

        EditorGUILayout.Space();

        foldoutMasking = EditorGUILayout.Foldout(foldoutMasking, "Masked objects (outside this mask hierarchy)");
        if (foldoutMasking)
        {
            EditorGUILayout.HelpBox("Masked objects are objects that are outside this mask and they should be masked by this mask. Each object (Transform) in the list and all their childs will masked out with this mask.", MessageType.Info);

            EditorGUI.indentLevel++;
            Transform trans = EditorGUILayout.ObjectField("Drop here >>>", null, typeof(Transform), true, options) as Transform;
            if (trans != null)
            {
                if (!mask.maskedObjects.Contains(trans))
                {
                    Debug.Log("Adding: " + trans.name);
                    mask.maskedObjects.Add(trans);
                    mask.SendMessage("Update", SendMessageOptions.DontRequireReceiver);
                }
                else
                {
                    Debug.LogWarning("Exist: " + trans.name);
                }
            }

            foldoutList = EditorGUILayout.Foldout(foldoutList, "All items: " + mask.maskedObjects.Count.ToString());
            if (foldoutList)
            {
                EditorGUI.indentLevel++;
                int deleteIndex = -1;
                for (int i = 0; i < mask.maskedObjects.Count; i++)
                {
                    EditorGUILayout.BeginHorizontal(options);
                    GUI.enabled = false;
                    EditorGUILayout.ObjectField(string.Concat("[", i.ToString(), "] >>>"), mask.maskedObjects [i], typeof(Transform), true);
                    GUI.enabled = true;
                    if (GUILayout.Button("Delete", options))
                    {
                        deleteIndex = i;
                    }
                    EditorGUILayout.EndHorizontal();
                }
                EditorGUI.indentLevel--;

                if (deleteIndex != -1)
                {
                    SpriteMaskingComponent smc = mask.maskedObjects [deleteIndex].GetComponent <SpriteMaskingComponent> ();
                    if (smc != null)
                    {
                        Debug.Log("Deleting: " + smc);

                        if (Application.isPlaying)
                        {
                            Destroy(smc);
                        }
                        else
                        {
                            DestroyImmediate(smc);
                        }
                    }
                    mask.maskedObjects.RemoveAt(deleteIndex);
                }
            }
            EditorGUI.indentLevel--;

            EditorGUILayout.Separator();
        }

        foldoutAdvance = EditorGUILayout.Foldout(foldoutAdvance, "Advance");
        if (foldoutAdvance)
        {
            EditorGUILayout.HelpBox("Forces to use this object default material on all his childs. This is useful when moving sprite between different SpriteMask objects.", MessageType.Info);
            bool currentForceDefaultMaterialOnChilds = mask.forceDefaultMaterialOnChilds;
            bool newForceDefaultMaterialOnChilds     = EditorGUILayout.Toggle("Force default material on childs", currentForceDefaultMaterialOnChilds, options);
            if (GUI.changed && currentForceDefaultMaterialOnChilds != newForceDefaultMaterialOnChilds)
            {
                Undo.RecordObject(target, "Force Materal Set To Default On Sprites change");
                mask.forceDefaultMaterialOnChilds = newForceDefaultMaterialOnChilds;
                EditorUtility.SetDirty(target);
            }

            EditorGUILayout.Space();

            EditorGUILayout.HelpBox("Forces to use individual material on all his childs. This is useful when you have many objects (masked and unmasked) with the same material.", MessageType.Info);
            bool currentForceIndividualMaterialOnChilds = mask.forceIndividualMaterialOnChilds;
            bool newForceIndividualMaterialOnChilds     = EditorGUILayout.Toggle("Force individual material on childs", currentForceIndividualMaterialOnChilds, options);
            if (GUI.changed && currentForceIndividualMaterialOnChilds != newForceIndividualMaterialOnChilds)
            {
                Undo.RecordObject(target, "Force Materal Set To Individual On Sprites change");
                mask.forceIndividualMaterialOnChilds = newForceIndividualMaterialOnChilds;
                EditorUtility.SetDirty(target);
            }

            EditorGUILayout.Space();

            Shader shader = mask.spritesShader;
            int    idx    = 0;
            if (customShader)
            {
                idx = 2;
            }
            else
            {
                if (shader != null)
                {
                    switch (shader.name)
                    {
                    case SpriteMaskTrueSoft.SHADER_SPRITE_DEFAULT:
                        idx = 0;
                        break;

                    case SpriteMaskTrueSoft.SHADER_SPRITE_DIFFUSE:
                        idx = 1;
                        break;

                    default:
                        idx          = 2;
                        customShader = true;
                        break;
                    }
                }
            }

            int newIdx = EditorGUILayout.Popup("Sprites shader", idx, shadersNames, options);

            Shader newShader = null;
            if (newIdx == 2)
            {
                customShader = true;
                EditorGUI.indentLevel++;
                newShader = EditorGUILayout.ObjectField("Shader", shader, typeof(Shader), true, options) as Shader;
                EditorGUI.indentLevel--;
            }
            else
            {
                customShader = false;
            }

            if (newIdx != idx)
            {
                switch (newIdx)
                {
                case 0:
                    newShader = Shader.Find(SpriteMaskTrueSoft.SHADER_SPRITE_DEFAULT);
                    break;

                case 1:
                    newShader = Shader.Find(SpriteMaskTrueSoft.SHADER_SPRITE_DIFFUSE);
                    break;
                }
            }

            if (newShader != null && shader != newShader)
            {
                Undo.RecordObject(target, "Shader change");
                mask.spritesShader = newShader;
                EditorUtility.SetDirty(target);
            }
        }

        string msg = string.Concat("Instance ID: ", mask.GetInstanceID(),
                                   "\nStencil ID: ", mask.stencilId,
                                   " (level=", mask.level,
                                   ", id=" + mask.maskIdPerLevel,
                                   ")");

        EditorGUILayout.HelpBox(msg, MessageType.None);
    }