Inheritance: UnityEditor.AssetPostprocessor
    void RepopulateWarnings()
    {
        warnings.Clear();

        if (skeletonJSON.objectReferenceValue == null)
        {
            warnings.Add("Missing Skeleton JSON");
        }
        else
        {
            if (SpineEditorUtilities.IsValidSpineData((TextAsset)skeletonJSON.objectReferenceValue) == false)
            {
                warnings.Add("Skeleton data file is not a valid JSON or binary file.");
            }
            else
            {
                bool detectedNullAtlasEntry = false;
                var  atlasList = new List <Atlas>();
                for (int i = 0; i < atlasAssets.arraySize; i++)
                {
                    if (atlasAssets.GetArrayElementAtIndex(i).objectReferenceValue == null)
                    {
                        detectedNullAtlasEntry = true;
                        break;
                    }
                    else
                    {
                        atlasList.Add(((AtlasAsset)atlasAssets.GetArrayElementAtIndex(i).objectReferenceValue).GetAtlas());
                    }
                }

                if (detectedNullAtlasEntry)
                {
                    warnings.Add("AtlasAsset elements cannot be Null");
                }
                else
                {
                    //get requirements
                    var missingPaths = SpineEditorUtilities.GetRequiredAtlasRegions(AssetDatabase.GetAssetPath((TextAsset)skeletonJSON.objectReferenceValue));

                    foreach (var atlas in atlasList)
                    {
                        for (int i = 0; i < missingPaths.Count; i++)
                        {
                            if (atlas.FindRegion(missingPaths[i]) != null)
                            {
                                missingPaths.RemoveAt(i);
                                i--;
                            }
                        }
                    }

                    foreach (var str in missingPaths)
                    {
                        warnings.Add("Missing Region: '" + str + "'");
                    }
                }
            }
        }
    }
    private void CreatePreviewInstances()
    {
        this.DestroyPreviewInstances();
        if (this.m_previewInstance == null)
        {
            try {
                string skinName = EditorPrefs.GetString(m_skeletonDataAssetGUID + "_lastSkin", "");

                m_previewInstance           = SpineEditorUtilities.InstantiateSkeletonAnimation((SkeletonDataAsset)target, skinName).gameObject;
                m_previewInstance.hideFlags = HideFlags.HideAndDontSave;
                m_previewInstance.layer     = 0x1f;


                m_skeletonAnimation = m_previewInstance.GetComponent <SkeletonAnimation>();
                m_skeletonAnimation.initialSkinName = skinName;
                m_skeletonAnimation.LateUpdate();

                m_skeletonData = m_skeletonAnimation.skeletonDataAsset.GetSkeletonData(true);

                m_previewInstance.GetComponent <Renderer>().enabled = false;

                m_initialized = true;
                AdjustCameraGoals(true);
            } catch {
            }
        }
    }
Example #3
0
    private void CreatePreviewInstances()
    {
        this.DestroyPreviewInstances();
        if (this.m_previewInstance == null)
        {
            string            skinName = EditorPrefs.GetString(m_skeletonDataAssetGUID + "_lastSkin", "");
            SkeletonAnimation skelAnim = SpineEditorUtilities.SpawnAnimatedSkeleton((SkeletonDataAsset)target, skinName);
            if (skelAnim == null)
            {
                failedSkeletonDataLatch = true;
                //EditorUtility.DisplayDialog("Skeleton / SpriteCollection Mismatch",  "GetSkeletonData failed, make sure JSON and SpriteCollection match.\nSprite Collection must contain all attachments referenced in SkeletonJSON", "OK");
                return;                 //failed
            }

            m_previewInstance = skelAnim.gameObject;
            //m_previewInstance.transform.localScale = Vector3.one * 0.01f;
            m_previewInstance.hideFlags = HideFlags.HideAndDontSave;
            m_previewInstance.layer     = 0x1f;


            m_skeletonAnimation = m_previewInstance.GetComponent <SkeletonAnimation>();
            m_skeletonAnimation.initialSkinName = skinName;
            m_skeletonAnimation.LateUpdate();
            m_skeletonData = m_skeletonAnimation.skeletonDataAsset.GetSkeletonData(true);

            m_previewInstance.renderer.enabled = false;

            m_initialized = true;
            AdjustCameraGoals(true);
        }
    }
    void OnEnable()
    {
        SpineEditorUtilities.ConfirmInitialization();

        try {
            atlasAssets   = serializedObject.FindProperty("atlasAssets");
            skeletonJSON  = serializedObject.FindProperty("skeletonJSON");
            scale         = serializedObject.FindProperty("scale");
            fromAnimation = serializedObject.FindProperty("fromAnimation");
            toAnimation   = serializedObject.FindProperty("toAnimation");
            duration      = serializedObject.FindProperty("duration");
            defaultMix    = serializedObject.FindProperty("defaultMix");
            controller    = serializedObject.FindProperty("controller");
#if SPINE_TK2D
            spriteCollection = serializedObject.FindProperty("spriteCollection");
#endif

            m_skeletonDataAsset     = (SkeletonDataAsset)target;
            m_skeletonDataAssetGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(m_skeletonDataAsset));

            EditorApplication.update += Update;
        } catch {
            // TODO: WARNING: empty catch block supresses errors.
        }

        m_skeletonData = m_skeletonDataAsset.GetSkeletonData(true);

        showUnity = EditorPrefs.GetBool("SkeletonDataAssetInspector_showUnity", true);

        RepopulateWarnings();
    }
Example #5
0
    private void Initialize(SkeletonDataAsset skeletonDataAsset, string skinName = "")
    {
        if (_previewRenderUtility != null)
        {
            return;
        }
        _previewRenderUtility = new PreviewRenderUtility(true);
        _lastANimationTime    = currentTime;

        if (_previewGameObject == null)
        {
            _previewGameObject = SpineEditorUtilities.SpawnAnimatedSkeleton(skeletonDataAsset, skinName).gameObject;

            if (_previewGameObject != null)
            {
                _previewGameObject.hideFlags       = HideFlags.HideAndDontSave;
                _previewGameObject.layer           = PreviewLayer;
                _skeletonAnimation                 = _previewGameObject.GetComponent <SkeletonAnimation>();
                _skeletonAnimation.initialSkinName = skinName;
                _skeletonAnimation.LateUpdate();
                //初始化时不进行渲染
                _previewGameObject.GetComponent <Renderer>().enabled = false;
            }
            _requireRefresh = true;
        }
        InitializePreviewCamera();
    }
    void OnEnable()
    {
        SpineEditorUtilities.ConfirmInitialization();

        try {
            atlasAssets   = serializedObject.FindProperty("atlasAssets");
            skeletonJSON  = serializedObject.FindProperty("skeletonJSON");
            scale         = serializedObject.FindProperty("scale");
            fromAnimation = serializedObject.FindProperty("fromAnimation");
            toAnimation   = serializedObject.FindProperty("toAnimation");
            duration      = serializedObject.FindProperty("duration");
            defaultMix    = serializedObject.FindProperty("defaultMix");
            controller    = serializedObject.FindProperty("controller");

            m_skeletonDataAsset     = (SkeletonDataAsset)target;
            m_skeletonDataAssetGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(m_skeletonDataAsset));

            EditorApplication.update += Update;
        } catch {
        }

        m_skeletonData = m_skeletonDataAsset.GetSkeletonData(true);

        showBaking = EditorPrefs.GetBool("SkeletonDataAssetInspector_showBaking", true);

        RepopulateWarnings();
    }
    void UpdateBakedList()
    {
        AtlasAsset asset = (AtlasAsset)target;

        baked        = new List <bool>();
        bakedObjects = new List <GameObject>();
        if (atlasFile.objectReferenceValue != null)
        {
            Atlas              atlas             = asset.GetAtlas();
            FieldInfo          field             = typeof(Atlas).GetField("regions", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.NonPublic);
            List <AtlasRegion> regions           = (List <AtlasRegion>)field.GetValue(atlas);
            string             atlasAssetPath    = AssetDatabase.GetAssetPath(atlasAsset);
            string             atlasAssetDirPath = Path.GetDirectoryName(atlasAssetPath);
            string             bakedDirPath      = Path.Combine(atlasAssetDirPath, atlasAsset.name);


            for (int i = 0; i < regions.Count; i++)
            {
                AtlasRegion region          = regions[i];
                string      bakedPrefabPath = Path.Combine(bakedDirPath, SpineEditorUtilities.GetPathSafeRegionName(region) + ".prefab").Replace("\\", "/");
                GameObject  prefab          = (GameObject)AssetDatabase.LoadAssetAtPath(bakedPrefabPath, typeof(GameObject));
                baked.Add(prefab != null);
                bakedObjects.Add(prefab);
            }
        }
    }
 void OnEnable()
 {
     SpineEditorUtilities.ConfirmInitialization();
     atlasFile  = serializedObject.FindProperty("atlasFile");
     materials  = serializedObject.FindProperty("materials");
     atlasAsset = (AtlasAsset)target;
     UpdateBakedList();
 }
 protected virtual void OnEnable()
 {
     SpineEditorUtilities.ConfirmInitialization();
     skeletonDataAsset  = serializedObject.FindProperty("skeletonDataAsset");
     initialSkinName    = serializedObject.FindProperty("initialSkinName");
     normals            = serializedObject.FindProperty("calculateNormals");
     tangents           = serializedObject.FindProperty("calculateTangents");
     meshes             = serializedObject.FindProperty("renderMeshes");
     immutableTriangles = serializedObject.FindProperty("immutableTriangles");
     submeshSeparators  = serializedObject.FindProperty("submeshSeparators");
 }
    void DoReimport()
    {
        SpineEditorUtilities.ImportSpineContent(new string[] { AssetDatabase.GetAssetPath(skeletonJSON.objectReferenceValue) }, true);

        if (m_previewUtility != null)
        {
            m_previewUtility.Cleanup();
            m_previewUtility = null;
        }

        RepopulateWarnings();
        OnEnable();

        EditorUtility.SetDirty(m_skeletonDataAsset);
    }
Example #11
0
    void DrawReimportButton()
    {
        EditorGUI.BeginDisabledGroup(skeletonJSON.objectReferenceValue == null);
        if (GUILayout.Button(new GUIContent("Attempt Reimport", SpineEditorUtilities.Icons.warning)))
        {
            SpineEditorUtilities.ImportSpineContent(new string[] { AssetDatabase.GetAssetPath(skeletonJSON.objectReferenceValue) }, true);

            if (m_previewUtility != null)
            {
                m_previewUtility.Cleanup();
                m_previewUtility = null;
            }

            RepopulateWarnings();
            OnEnable();
            return;
        }
        EditorGUI.EndDisabledGroup();
    }
    protected virtual void OnEnable()
    {
        SpineEditorUtilities.ConfirmInitialization();
        skeletonDataAsset  = serializedObject.FindProperty("skeletonDataAsset");
        initialSkinName    = serializedObject.FindProperty("initialSkinName");
        normals            = serializedObject.FindProperty("calculateNormals");
        tangents           = serializedObject.FindProperty("calculateTangents");
        meshes             = serializedObject.FindProperty("renderMeshes");
        immutableTriangles = serializedObject.FindProperty("immutableTriangles");
        submeshSeparators  = serializedObject.FindProperty("submeshSeparators");
        front = serializedObject.FindProperty("frontFacing");

        if (EditorGUILayoutSortingLayerField == null)
        {
            EditorGUILayoutSortingLayerField = typeof(EditorGUILayout).GetMethod("SortingLayerField", BindingFlags.Static | BindingFlags.NonPublic, null, new Type[] { typeof(GUIContent), typeof(SerializedProperty), typeof(GUIStyle) }, null);
        }

        rendererSerializedObject = new SerializedObject(((SkeletonRenderer)target).GetComponent <Renderer>());
        sortingLayerIDProperty   = rendererSerializedObject.FindProperty("m_SortingLayerID");
    }
    static AtlasAsset IngestSpineAtlas(TextAsset atlasText)
    {
        if (atlasText == null)
        {
            Debug.LogWarning("Atlas source cannot be null!");
            return(null);
        }

        string primaryName = Path.GetFileNameWithoutExtension(atlasText.name).Replace(".atlas", "");
        string assetPath   = Path.GetDirectoryName(AssetDatabase.GetAssetPath(atlasText));

        string atlasPath = assetPath + "/" + primaryName + "_Atlas.asset";

        AtlasAsset atlasAsset = (AtlasAsset)AssetDatabase.LoadAssetAtPath(atlasPath, typeof(AtlasAsset));

        List <Material> vestigialMaterials = new List <Material>();

        if (atlasAsset == null)
        {
            atlasAsset = AtlasAsset.CreateInstance <AtlasAsset>();
        }
        else
        {
            foreach (Material m in atlasAsset.materials)
            {
                vestigialMaterials.Add(m);
            }
        }

        atlasAsset.atlasFile = atlasText;

        //strip CR
        string atlasStr = atlasText.text;

        atlasStr = atlasStr.Replace("\r", "");

        string[]      atlasLines = atlasStr.Split('\n');
        List <string> pageFiles  = new List <string>();

        for (int i = 0; i < atlasLines.Length - 1; i++)
        {
            if (atlasLines[i].Length == 0)
            {
                pageFiles.Add(atlasLines[i + 1]);
            }
        }

        atlasAsset.materials = new Material[pageFiles.Count];

        for (int i = 0; i < pageFiles.Count; i++)
        {
            string    texturePath = assetPath + "/" + pageFiles[i];
            Texture2D texture     = (Texture2D)AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D));

            TextureImporter texImporter = (TextureImporter)TextureImporter.GetAtPath(texturePath);
            texImporter.textureFormat       = TextureImporterFormat.AutomaticTruecolor;
            texImporter.mipmapEnabled       = false;
            texImporter.alphaIsTransparency = false;
            texImporter.maxTextureSize      = 2048;

            EditorUtility.SetDirty(texImporter);
            AssetDatabase.ImportAsset(texturePath);
            AssetDatabase.SaveAssets();

            string pageName = Path.GetFileNameWithoutExtension(pageFiles[i]);

            //because this looks silly
            if (pageName == primaryName && pageFiles.Count == 1)
            {
                pageName = "Material";
            }

            string   materialPath = assetPath + "/" + primaryName + "_" + pageName + ".mat";
            Material mat          = (Material)AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material));

            if (mat == null)
            {
                mat = new Material(Shader.Find(defaultShader));
                AssetDatabase.CreateAsset(mat, materialPath);
            }
            else
            {
                vestigialMaterials.Remove(mat);
            }

            mat.mainTexture = texture;
            EditorUtility.SetDirty(mat);

            AssetDatabase.SaveAssets();

            atlasAsset.materials[i] = mat;
        }

        for (int i = 0; i < vestigialMaterials.Count; i++)
        {
            AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(vestigialMaterials[i]));
        }

        if (AssetDatabase.GetAssetPath(atlasAsset) == "")
        {
            AssetDatabase.CreateAsset(atlasAsset, atlasPath);
        }
        else
        {
            atlasAsset.Reset();
        }

        EditorUtility.SetDirty(atlasAsset);

        AssetDatabase.SaveAssets();


        //iterate regions and bake marked
        Atlas              atlas             = atlasAsset.GetAtlas();
        FieldInfo          field             = typeof(Atlas).GetField("regions", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.NonPublic);
        List <AtlasRegion> regions           = (List <AtlasRegion>)field.GetValue(atlas);
        string             atlasAssetPath    = AssetDatabase.GetAssetPath(atlasAsset);
        string             atlasAssetDirPath = Path.GetDirectoryName(atlasAssetPath);
        string             bakedDirPath      = Path.Combine(atlasAssetDirPath, atlasAsset.name);

        bool hasBakedRegions = false;

        for (int i = 0; i < regions.Count; i++)
        {
            AtlasRegion region          = regions[i];
            string      bakedPrefabPath = Path.Combine(bakedDirPath, SpineEditorUtilities.GetPathSafeRegionName(region) + ".prefab").Replace("\\", "/");
            GameObject  prefab          = (GameObject)AssetDatabase.LoadAssetAtPath(bakedPrefabPath, typeof(GameObject));

            if (prefab != null)
            {
                BakeRegion(atlasAsset, region, false);
                hasBakedRegions = true;
            }
        }

        if (hasBakedRegions)
        {
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }

        return((AtlasAsset)AssetDatabase.LoadAssetAtPath(atlasPath, typeof(AtlasAsset)));
    }
 void OnEnable()
 {
     SpineEditorUtilities.ConfirmInitialization();
     atlasFile = serializedObject.FindProperty("atlasFile");
     materials = serializedObject.FindProperty("materials");
 }
    override public void OnInspectorGUI()
    {
        serializedObject.Update();
        AtlasAsset asset = (AtlasAsset)target;

        EditorGUI.BeginChangeCheck();
        EditorGUILayout.PropertyField(atlasFile);
        EditorGUILayout.PropertyField(materials, true);
        if (EditorGUI.EndChangeCheck())
        {
            serializedObject.ApplyModifiedProperties();
        }

        if (materials.arraySize == 0)
        {
            EditorGUILayout.LabelField(new GUIContent("Error:  Missing materials", SpineEditorUtilities.Icons.warning));
            return;
        }

        for (int i = 0; i < materials.arraySize; i++)
        {
            SerializedProperty prop = materials.GetArrayElementAtIndex(i);
            Material           mat  = (Material)prop.objectReferenceValue;
            if (mat == null)
            {
                EditorGUILayout.LabelField(new GUIContent("Error:  Materials cannot be null", SpineEditorUtilities.Icons.warning));
                return;
            }
        }

        if (atlasFile.objectReferenceValue != null)
        {
            Atlas              atlas   = asset.GetAtlas();
            FieldInfo          field   = typeof(Atlas).GetField("regions", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.NonPublic);
            List <AtlasRegion> regions = (List <AtlasRegion>)field.GetValue(atlas);
            EditorGUILayout.LabelField(new GUIContent("Region Baking", SpineEditorUtilities.Icons.unityIcon));
            EditorGUI.indentLevel++;
            AtlasPage lastPage = null;
            for (int i = 0; i < regions.Count; i++)
            {
                if (lastPage != regions[i].page)
                {
                    if (lastPage != null)
                    {
                        EditorGUILayout.Separator();
                        EditorGUILayout.Separator();
                    }
                    lastPage = regions[i].page;
                    Material mat = ((Material)lastPage.rendererObject);
                    if (mat != null)
                    {
                        GUILayout.BeginHorizontal();
                        {
                            EditorGUI.BeginDisabledGroup(true);
                            EditorGUILayout.ObjectField(mat, typeof(Material), false, GUILayout.Width(250));
                            EditorGUI.EndDisabledGroup();
                        }
                        GUILayout.EndHorizontal();
                    }
                    else
                    {
                        EditorGUILayout.LabelField(new GUIContent("Page missing material!", SpineEditorUtilities.Icons.warning));
                    }
                }
                GUILayout.BeginHorizontal();
                {
                    //EditorGUILayout.ToggleLeft(baked[i] ? "" : regions[i].name, baked[i]);
                    bool result = baked[i] ? EditorGUILayout.ToggleLeft("", baked[i], GUILayout.Width(24)) : EditorGUILayout.ToggleLeft("    " + regions[i].name, baked[i]);
                    if (baked[i])
                    {
                        EditorGUILayout.ObjectField(bakedObjects[i], typeof(GameObject), false, GUILayout.Width(250));
                    }
                    if (result && !baked[i])
                    {
                        //bake
                        baked[i]        = true;
                        bakedObjects[i] = SpineEditorUtilities.BakeRegion(atlasAsset, regions[i]);
                        EditorGUIUtility.PingObject(bakedObjects[i]);
                    }
                    else if (!result && baked[i])
                    {
                        //unbake
                        bool unbakeResult = EditorUtility.DisplayDialog("Delete Baked Region", "Do you want to delete the prefab for " + regions[i].name, "Yes", "Cancel");
                        switch (unbakeResult)
                        {
                        case true:
                            //delete
                            string atlasAssetPath    = AssetDatabase.GetAssetPath(atlasAsset);
                            string atlasAssetDirPath = Path.GetDirectoryName(atlasAssetPath);
                            string bakedDirPath      = Path.Combine(atlasAssetDirPath, atlasAsset.name);
                            string bakedPrefabPath   = Path.Combine(bakedDirPath, SpineEditorUtilities.GetPathSafeRegionName(regions[i]) + ".prefab").Replace("\\", "/");
                            AssetDatabase.DeleteAsset(bakedPrefabPath);
                            baked[i] = false;
                            break;

                        case false:
                            //do nothing
                            break;
                        }
                    }
                }
                GUILayout.EndHorizontal();
            }
            EditorGUI.indentLevel--;
        }

        if (serializedObject.ApplyModifiedProperties() ||
            (UnityEngine.Event.current.type == EventType.ValidateCommand && UnityEngine.Event.current.commandName == "UndoRedoPerformed")
            )
        {
            asset.Reset();
        }
    }