Пример #1
0
        void UpdatePivot(SpriteFrame sprite, int selectPivot)
        {
            m_pivotIndex = selectPivot;
            sprite.CreateQuad();
            switch (selectPivot)
            {
            case 1:
                sprite.pivot = new Vector2(0.5f, 0.5f);
                break;

            case 2:
                sprite.pivot = new Vector2(0.5f, 1f);
                break;

            case 3:
                sprite.pivot = new Vector2(0f, 1f);
                break;

            case 4:
                sprite.pivot = new Vector2(1f, 1f);
                break;

            case 5:
                sprite.pivot = new Vector2(0f, 0.5f);
                break;

            case 6:
                sprite.pivot = new Vector2(1f, 0.5f);
                break;

            case 7:
                sprite.pivot = new Vector2(0.5f, 0f);
                break;

            case 8:
                sprite.pivot = new Vector2(0f, 0f);
                break;

            case 9:
                sprite.pivot = new Vector2(1f, 0f);
                break;
            }
        }
Пример #2
0
        /// <summary>
        /// Changes the sprite frame.
        /// </summary>
        /// <param name="sf">Will replace SpriteFrame</param>
        /// <param name="texture">new Texture.</param>
        /// <param name="mat">Mat.</param>
        public void ChangeSpriteFrame(SpriteFrame sf, Texture texture, Material mat = null, bool useMaterialBlock = true)
        {
            if (!sf || !texture)
            {
                return;
            }

            //new textureframe
            TextureFrame frame = new TextureFrame();

            frame.atlasTextureSize = new Vector2(texture.width, texture.height);
            frame.material         = mat == null ? sf.material : mat;
            frame.rect.x           = 0f;
            frame.rect.y           = 0f;
            frame.rect.width       = texture.width;
            frame.rect.height      = texture.height;
            frame.frameSize        = new Rect(0, 0, texture.width, texture.height);
            frame.isRotated        = false;
            if (!sf.isCreatedMesh)
            {
                sf.CreateQuad();
            }
            sf.frame = frame;
            if (mat)
            {
                sf.material = mat;
            }

            //change texture
            if (useMaterialBlock)
            {
                sf.meshRenderer.GetPropertyBlock(materialPropertyBlock);
                m_MatBlock.SetTexture("_MainTex", texture);
                sf.meshRenderer.SetPropertyBlock(materialPropertyBlock);
            }
            else
            {
                sf.material.mainTexture = texture;
            }
        }
Пример #3
0
 static void CreateSpriteFrame()
 {
     if (Selection.activeObject is TextureFrames)
     {
         TextureFrames tfs = AssetDatabase.LoadAssetAtPath <TextureFrames>(AssetDatabase.GetAssetPath(Selection.activeObject));
         if (tfs && tfs.frames != null && tfs.frames.Length > 0)
         {
             GameObject  go = new GameObject();
             SpriteFrame sf = go.AddComponent <SpriteFrame>();
             sf.CreateQuad();
             sf.textureFrames = tfs;
             foreach (Material mat in tfs.materials)
             {
                 if (mat.name.LastIndexOf("_UI_Mat") == -1)
                 {
                     sf.material = tfs.materials[0];
                     break;
                 }
             }
         }
     }
 }
Пример #4
0
        public override void OnInspectorGUI()
        {
            SpriteFrame sprite = target as SpriteFrame;

            serializedObject.Update();
            EditorGUILayout.BeginVertical();
            EditorGUILayout.PropertyField(serializedObject.FindProperty("textureFrames"), true);
            if (sprite.textureFrames != null && sprite.textureFrames.frames.Length > 0)
            {
                //显示frameName列表
                string[] list = new string[sprite.textureFrames.frames.Length];
                for (int i = 0; i < sprite.textureFrames.frames.Length; ++i)
                {
                    list[i] = sprite.textureFrames.frames[i].name;
                }
                int selectIndex = EditorGUILayout.Popup("Frame", m_frameIndex, list);
                if (selectIndex != m_frameIndex)
                {
                    m_frameIndex = selectIndex;
                    sprite.CreateQuad();
                    sprite.frameName = sprite.textureFrames.frames[m_frameIndex].name;
                    UpdatePivot(sprite, m_pivotIndex);
                }
            }
            EditorGUILayout.EndVertical();
            EditorGUILayout.Space();
            EditorGUILayout.PropertyField(serializedObject.FindProperty("m_skew"), true);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("m_uvOffset"), true);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("m_pivot"), true);
            if (sprite.textureFrames != null && !string.IsNullOrEmpty(sprite.frameName))
            {
                //			"None","CENTER","TOP","TOP_LEFT","TOP_RIGHT",
                //			"LEFT","RIGHT","BOTTOM","BOTTOM_LEFT","BOTTOM_RIGHT"
                int selectPivot = EditorGUILayout.Popup("Auto Pivot", m_pivotIndex, posList);
                if (selectPivot != m_pivotIndex)
                {
                    UpdatePivot(sprite, selectPivot);
                    sprite.frameName = sprite.frameName;
                }
            }
            EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Color"), true);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("m_brightness"), true);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("m_PreMultiplyAlpha"), true);
            selectedOption = EditorGUILayout.Popup("Sorting Layer", selectedOption, sortingLayerNames);
            if (sortingLayerNames[selectedOption] != sprite.sortingLayerName)
            {
                Undo.RecordObject(sprite, "Sorting Layer");
                sprite.sortingLayerName = sortingLayerNames[selectedOption];
                EditorUtility.SetDirty(sprite);
                if (!Application.isPlaying && !string.IsNullOrEmpty(sprite.gameObject.scene.name))
                {
                    UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
                }
            }
            int newSortingLayerOrder = EditorGUILayout.IntField("Order in Layer", sprite.sortingOrder);

            if (newSortingLayerOrder != sprite.sortingOrder)
            {
                Undo.RecordObject(sprite, "Edit Sorting Order");
                sprite.sortingOrder = newSortingLayerOrder;
                EditorUtility.SetDirty(sprite);
                if (!Application.isPlaying && !string.IsNullOrEmpty(sprite.gameObject.scene.name))
                {
                    UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
                }
            }

            serializedObject.ApplyModifiedProperties();
        }
Пример #5
0
        private static void ShowSpriteFrame(TextureFrame frame, SpineData.SkinAttachment attachmentData, Transform slot, Transform skinParent, SpineArmatureEditor armatureEditor)
        {
            GameObject  go       = new GameObject();
            SpriteFrame newFrame = go.AddComponent <SpriteFrame>();

            newFrame.CreateQuad();
            newFrame.textureFrames    = armatureEditor.m_TextureFrames;
            newFrame.frame            = frame;
            newFrame.name             = attachmentData.name;
            newFrame.pivot            = Vector2.one * 0.5f;
            newFrame.transform.parent = skinParent;

            Vector3 localPos = Vector3.zero;

            localPos.x = attachmentData.x;
            localPos.y = attachmentData.y;
            newFrame.transform.localPosition = localPos;

            Vector3 localSc = Vector3.one;

            localSc.x = attachmentData.scaleX;
            localSc.y = attachmentData.scaleY;

            if (newFrame.frame.isRotated)
            {
                if (attachmentData.width > 0 && frame.rect.height > 0)
                {
                    localSc.x *= attachmentData.width / frame.rect.height;
                }
                if (attachmentData.height > 0 && frame.rect.width > 0)
                {
                    localSc.y *= attachmentData.height / frame.rect.width;
                }
            }
            else
            {
                if (attachmentData.width > 0 && frame.rect.width > 0)
                {
                    localSc.x *= attachmentData.width / frame.rect.width;
                }
                if (attachmentData.height > 0 && frame.rect.height > 0)
                {
                    localSc.y *= attachmentData.height / frame.rect.height;
                }
            }

            newFrame.transform.localScale = localSc;
            newFrame.color = attachmentData.color;
            newFrame.transform.localRotation = Quaternion.Euler(0, 0, attachmentData.rotation);

            if (armatureEditor.genImgCollider)
            {
                BoxCollider2D collider = newFrame.gameObject.AddComponent <BoxCollider2D>();
                if (newFrame.frame.isRotated)
                {
                    collider.size = new Vector2(newFrame.frame.rect.size.y, newFrame.frame.rect.size.x) * armatureEditor.unit;

                    Vector2 center = new Vector2(
                        -newFrame.frame.frameSize.width / 2 - newFrame.frame.frameSize.x + newFrame.frame.rect.width / 2,
                        newFrame.frame.frameSize.height / 2 + newFrame.frame.frameSize.y - newFrame.frame.rect.height / 2);
                    collider.offset = center * armatureEditor.unit;
                }
                else
                {
                    collider.size = newFrame.frame.rect.size * armatureEditor.unit;

                    Vector2 center = new Vector2(
                        -newFrame.frame.frameSize.width / 2 - newFrame.frame.frameSize.x + newFrame.frame.rect.width / 2,
                        newFrame.frame.frameSize.height / 2 + newFrame.frame.frameSize.y - newFrame.frame.rect.height / 2);
                    collider.offset = center * armatureEditor.unit;
                }
            }
            newFrame.UpdateVertexColor();
        }