Пример #1
0
    void SaveMaterial(Material addMat)
    {
        string path    = "";
        string newPath = "";
        string uniquePath;
        Object existsObj;
        int    nUniqueCount = 0;

        // Find Same Material
        if (m_bUniqueSave == false && m_SelectedTextureMaterials != null)
        {
            foreach (Material mat in m_SelectedTextureMaterials)
            {
                if (NgMaterial.IsSameMaterial(mat, addMat, false))
                {
                    NgUtil.LogMessage(FXMakerTooltip.GetHsToolMessage("MATERIAL_EXISTSAVED", ""));
                    SetSelectedTransformMaterial(mat);
                    return;
                }
            }
        }

        // Create Path
        bool   bUseDefaultFolder = false;
        string devMatDir         = "_MaterialsTool";
        string userMatDir        = "_MaterialsUser";
        string matDir;

        if (FXMakerLayout.m_bDevelopState)
        {
            matDir = devMatDir;
        }
        else
        {
            matDir = userMatDir;
        }

        if ((addMat.mainTexture != null))
        {
            path = AssetDatabase.GetAssetPath(addMat.mainTexture);
            if (path == "")
            {
                bUseDefaultFolder = true;
            }
            else
            {
                newPath = NgFile.CombinePath(NgFile.TrimFilenameExt(path), matDir);
                // Default SubDirectory
                if (NgAsset.ExistsDirectory(newPath) == false)
                {
                    AssetDatabase.CreateFolder(NgFile.TrimFilenameExt(path), matDir);
                }
            }
        }
        else
        {
            newPath = NgFile.TrimFilenameExt(AssetDatabase.GetAssetPath(m_OriMaterial));
            if (newPath == "")
            {
                bUseDefaultFolder = true;
            }
            else
            {
                string tmpPath = NgFile.TrimLastFolder(newPath);
                string tmpLast = NgFile.GetLastFolder(newPath);

                if (FXMakerLayout.m_bDevelopState)
                {
                    if (tmpLast != devMatDir)
                    {
                        newPath = NgFile.CombinePath(tmpPath, devMatDir);
                        if (NgAsset.ExistsDirectory(newPath) == false)
                        {
                            AssetDatabase.CreateFolder(tmpPath, matDir);
                        }
                    }
                }
                else
                {
                    if (tmpLast != userMatDir)
                    {
                        newPath = NgFile.CombinePath(tmpPath, userMatDir);
                        if (NgAsset.ExistsDirectory(newPath) == false)
                        {
                            AssetDatabase.CreateFolder(tmpPath, matDir);
                        }
                    }
                }
            }
        }

        if (bUseDefaultFolder)
        {
            path    = FXMakerMain.inst.GetResourceDir(FXMakerMain.TOOLDIR_TYPE.BACKGROUNDRESOURCES);
            newPath = NgFile.CombinePath(path, matDir);
            // Default SubDirectory
            if (NgAsset.ExistsDirectory(newPath) == false)
            {
                AssetDatabase.CreateFolder(path, matDir);
            }
        }

        // Unique Name
        string texname = addMat.name;

        if (addMat.mainTexture != null)
        {
            texname = addMat.mainTexture.name;
        }
        int nLoopCount = 0;

        while (true)
        {
            string matName = texname + (0 < nUniqueCount ? "_" + nUniqueCount.ToString() : "") + ".mat";
            uniquePath = NgFile.CombinePath(newPath, matName);
            existsObj  = AssetDatabase.LoadAssetAtPath(uniquePath, typeof(Material));
            if (existsObj == null)
            {
                break;
            }
            nLoopCount++;
            nUniqueCount++;
            if (999 < nUniqueCount)
            {
                nUniqueCount = 1;
            }
            if (999 < nLoopCount)
            {
                Debug.LogError("Over Loop ----------------------");
                return;
            }
        }

        AssetDatabase.CreateAsset(addMat, uniquePath);
        NgUtil.LogMessage(FXMakerTooltip.GetHsToolMessage("MATERIAL_NEWSAVED", "") + "\n" + uniquePath);
        FXMakerAsset.AssetDatabaseRefresh();
        FXMakerAsset.AssetDatabaseSaveAssets();
    }