Exemplo n.º 1
0
        void DrawSpriteSecondaryTextureElement(Rect rect, int index, bool isActive, bool isFocused)
        {
            bool  dataModified  = false;
            float oldLabelWidth = EditorGUIUtility.labelWidth;

            EditorGUIUtility.labelWidth = 70.0f;
            SecondarySpriteTexture secondaryTexture = secondaryTextureList[index];

            // "Name" text field
            EditorGUI.BeginChangeCheck();
            var    r       = new Rect(rect.x, rect.y + 5, rect.width - Styles.textFieldDropDownWidth, EditorGUIUtility.singleLineHeight);
            string newName = EditorGUI.TextField(r, Styles.name, secondaryTexture.name);

            dataModified = EditorGUI.EndChangeCheck();

            // Suggested names
            if (m_SuggestedNames != null)
            {
                var popupRect = new Rect(r.x + r.width, r.y, Styles.textFieldDropDownWidth, EditorGUIUtility.singleLineHeight);
                EditorGUI.BeginChangeCheck();
                int selected = EditorGUI.Popup(popupRect, -1, m_SuggestedNames, EditorStyles.textFieldDropDown);
                if (EditorGUI.EndChangeCheck())
                {
                    newName      = m_SuggestedNames[selected];
                    dataModified = true;
                }
            }

            if (dataModified)
            {
                if (!string.IsNullOrEmpty(newName) && newName != secondaryTexture.name && secondaryTextureList.Exists(x => x.name == newName))
                {
                    Debug.LogWarning(Styles.nameUniquenessWarning);
                }
                else if (newName == "_MainTex" || newName == "_AlphaTex")
                {
                    Debug.LogWarning(Styles.builtInNameCollisionWarning);
                }
                else
                {
                    secondaryTexture.name = newName;
                }
            }

            // "Texture" object field
            EditorGUI.BeginChangeCheck();
            r.width = rect.width;
            r.y    += EditorGUIUtility.singleLineHeight;
            secondaryTexture.texture = EditorGUI.ObjectField(r, Styles.texture, secondaryTexture.texture, typeof(Texture2D), false) as Texture2D;
            dataModified             = dataModified || EditorGUI.EndChangeCheck();

            if (dataModified)
            {
                secondaryTextureList[index] = secondaryTexture;
                spriteEditor.SetDataModified();
            }

            EditorGUIUtility.labelWidth = oldLabelWidth;
        }
Exemplo n.º 2
0
        public static SecondarySpriteTexture[] Load(SerializedObject so)
        {
            var secondaryTextures = so.FindProperty("m_SpriteSheet.m_SecondaryTextures");
            var returnValue       = new SecondarySpriteTexture[secondaryTextures.arraySize];

            for (int i = 0; i < returnValue.Length; ++i)
            {
                var sp = secondaryTextures.GetArrayElementAtIndex(i);
                returnValue[i].name    = sp.FindPropertyRelative("name").stringValue;
                returnValue[i].texture = sp.FindPropertyRelative("texture").objectReferenceValue as Texture2D;
            }
            return(returnValue);
        }
Exemplo n.º 3
0
    public static void AddSecondaryTextureToSprite()
    {
        // filter selection to assets only
        Object[] spriteAssets = Selection.GetFiltered(typeof(Object), SelectionMode.Assets);

        // process all assets
        foreach (Object item in spriteAssets)
        {
            // path to selected sprite asset
            string spriteAssetPath = AssetDatabase.GetAssetPath(item);


            // find normal texture in subfolders
            string   normalTextureName = Path.GetFileNameWithoutExtension(spriteAssetPath) + NORMAL_MAP_SUFFIX;
            string   normalTextureType = " t:Texture2D";
            string[] searchFolders     = AssetDatabase.GetSubFolders(Path.GetDirectoryName(spriteAssetPath));
            string[] normalGUIDs       = AssetDatabase.FindAssets(normalTextureName + normalTextureType, searchFolders);

            // if no normal, then log warrning
            if (normalGUIDs == null || normalGUIDs.Length == 0)
            {
                Debug.LogWarning("Not found " + normalTextureName + " in subfolders");
                continue;
            }


            // get texture importer for current sprite asset
            TextureImporter importer = AssetImporter.GetAtPath(spriteAssetPath) as TextureImporter;

            // create secondary texture entries array
            SecondarySpriteTexture[] secondarySpriteTextures = new SecondarySpriteTexture[] {
                new SecondarySpriteTexture {
                    name    = SECONDARY_TEXTURE_NAME_NORMAL,
                    texture = AssetDatabase.LoadAssetAtPath <Texture2D>(AssetDatabase.GUIDToAssetPath(normalGUIDs[0]))
                }
            };
            // set new array to importer
            importer.secondarySpriteTextures = secondarySpriteTextures;


            // save importer
            EditorUtility.SetDirty(importer);
            importer.SaveAndReimport();
        }
    }
Exemplo n.º 4
0
        public static bool SetSecondaryTexture(string mainTexFullPath, List <SecTextureData> secTextureDatas,
                                               out List <string> message)
        {
            var importer = GetImporter(mainTexFullPath) as TextureImporter;
            var secondarySpriteTextures = new SecondarySpriteTexture[secTextureDatas.Count];

            message = new List <string>();
            for (var i = 0; i < secTextureDatas.Count; i++)
            {
                var secTextureData = secTextureDatas[i];
                var texturePath    = secTextureData.AssetPath;
                var texture2D      = AssetDatabase.LoadAssetAtPath <Texture2D>(texturePath);
                if (texture2D == null || secTextureData.Name == string.Empty)
                {
                    message.Add($"Name:{secTextureData.Name} Path:{texturePath}");
                    continue;
                }

                var secondarySpriteTexture = new SecondarySpriteTexture
                {
                    name = secTextureData.Name, texture = texture2D
                };
                secondarySpriteTextures[i] = secondarySpriteTexture;
            }

            var success = message.Count == 0 && secTextureDatas.Count > 0 &&
                          importer != null;

            if (success)
            {
                importer.secondarySpriteTextures = secondarySpriteTextures;
                importer.SaveAndReimport();
            }

            return(success);
        }