//
    public static MeshAssetRule CreateAssetRule()
    {
        MeshAssetRule assetRule = CreateInstance <MeshAssetRule>();

        assetRule.ApplyDefaults();
        return(assetRule);
    }
    public override void OnInspectorGUI()
    {
        MeshAssetRule t = (MeshAssetRule)target;

        EditorGUI.BeginChangeCheck();

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("MeshImportRules");
        EditorGUILayout.EndHorizontal();


        DrawMeshSettings(t);

        if (EditorGUI.EndChangeCheck())
        {
            changed = true;
        }

        if (changed)
        {
            if (GUILayout.Button("Apply"))
            {
                Apply(t);
            }
            EditorUtility.SetDirty(t);
        }
    }
    public static void CreateAssetRule()
    {
        MeshAssetRule newRule = CreateInstance <MeshAssetRule>();

        newRule.ApplyDefaults();

        string selectionpath = "Assets";

        foreach (Object obj in Selection.GetFiltered(typeof(Object), SelectionMode.Assets))
        {
            selectionpath = AssetDatabase.GetAssetPath(obj);
            Debug.Log("selectionpath in foreach: " + selectionpath);
            if (File.Exists(selectionpath))
            {
                Debug.Log("File.Exists: " + selectionpath);
                selectionpath = Path.GetDirectoryName(selectionpath);
            }
            break;
        }

        Debug.Log("selectionpath: " + selectionpath);
        string newRuleFileName = AssetDatabase.GenerateUniqueAssetPath(Path.Combine(selectionpath, "New Mesh Rule.asset"));

        newRuleFileName = newRuleFileName.Replace("\\", "/");
        AssetDatabase.CreateAsset(newRule, newRuleFileName);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        EditorUtility.FocusProjectWindow();
        Selection.activeObject = newRule;
        changed = true;
    }
    private void Apply(MeshAssetRule assetRule)
    {
        //get the directories that we do not want to apply changes to
        List <string> dontapply     = new List <string>();
        string        assetrulepath = AssetDatabase.GetAssetPath(assetRule).Replace(assetRule.name + ".asset", "").TrimEnd('/');
        string        projPath      = Application.dataPath;

        projPath = projPath.Remove(projPath.Length - 6);

        string[] directories = Directory.GetDirectories(Path.GetDirectoryName(projPath + AssetDatabase.GetAssetPath(assetRule)), "*", SearchOption.AllDirectories);
        foreach (string directory in directories)
        {
            string   d       = directory.Replace(Application.dataPath, "Assets");
            string[] appDirs = AssetDatabase.FindAssets("t:AssetRule", new[] { d });
            if (appDirs.Length != 0)
            {
                d = d.TrimEnd('/');
                d = d.Replace('\\', '/');
                dontapply.Add(d);
            }
        }

        List <string> finalAssetList = new List <string>();

        foreach (string findAsset in AssetDatabase.FindAssets("", new[] { assetrulepath }))
        {
            string asset = AssetDatabase.GUIDToAssetPath(findAsset);
            if (!File.Exists(asset))
            {
                continue;
            }
            if (dontapply.Contains(Path.GetDirectoryName(asset)))
            {
                continue;
            }
            if (!assetRule.IsMatch(AssetImporter.GetAtPath(asset)))
            {
                continue;
            }
            if (finalAssetList.Contains(asset))
            {
                continue;
            }
            if (asset == AssetDatabase.GetAssetPath(assetRule))
            {
                continue;
            }
            finalAssetList.Add(asset);
        }

        foreach (string asset in finalAssetList)
        {
            AssetImporter.GetAtPath(asset).SaveAndReimport();
        }

        changed = false;
    }
    void OnEnable()
    {
        if (styles == null)
        {
            styles = new Styles();
        }

        changed = false;
        orig    = (MeshAssetRule)target;

        Undo.RecordObject(target, "assetruleundo");
    }
    private static void ExcuteMeshRule(AssetImporter importer)
    {
        MeshAssetRule rule = FindRuleForMeshAsset <MeshAssetRule>(importer.assetPath, "t:MeshAssetRule");

        if (rule == null)
        {
            Debug.Log("No asset rules found for asset");
        }
        else
        {
            Debug.Log("Begin to Applay Mesh Settings");
            rule.ApplyMeshSettings(importer);
        }
    }
    private void NormalsAndTangentsGUI(MeshAssetRule rule)
    {
        GUILayout.Label(styles.TangentSpace, EditorStyles.boldLabel, new GUILayoutOption[0]);

        EditorGUI.BeginChangeCheck();
        rule.normalImportMode = (ModelImporterNormals)EditorGUILayout.EnumPopup(styles.TangentSpaceNormalLabel, rule.normalImportMode);
        if (EditorGUI.EndChangeCheck())
        {
            if (rule.normalImportMode == ModelImporterNormals.None)
            {
                rule.tangentImportMode = ModelImporterTangents.None;
            }
            else if (rule.normalImportMode == ModelImporterNormals.Import)
            {
                rule.tangentImportMode = ModelImporterTangents.Import;
            }
            else
            {
                rule.tangentImportMode = ModelImporterTangents.CalculateMikk;
            }
        }

        using (new EditorGUI.DisabledScope(rule.normalImportMode != ModelImporterNormals.Calculate))
        {
            rule.normalSmoothAngle = EditorGUILayout.Slider(styles.SmoothingAngle, rule.normalSmoothAngle, 0f, 180f);
        }
        GUIContent[]            displayedOptions = styles.TangentSpaceModeOptLabelsAll;
        ModelImporterTangents[] array            = styles.TangentSpaceModeOptEnumsAll;
        if (rule.normalImportMode == ModelImporterNormals.Calculate)
        {
            displayedOptions = styles.TangentSpaceModeOptLabelsCalculate;
            array            = styles.TangentSpaceModeOptEnumsCalculate;
        }
        else if (rule.normalImportMode == ModelImporterNormals.None)
        {
            displayedOptions = styles.TangentSpaceModeOptLabelsNone;
            array            = styles.TangentSpaceModeOptEnumsNone;
        }
        using (new EditorGUI.DisabledScope(rule.normalImportMode == ModelImporterNormals.None))
        {
            int num = Array.IndexOf(array, rule.tangentImportMode);
            EditorGUI.BeginChangeCheck();
            num = EditorGUILayout.Popup(styles.TangentSpaceTangentLabel, num, displayedOptions, new GUILayoutOption[0]);
            if (EditorGUI.EndChangeCheck())
            {
                rule.tangentImportMode = array[num];
            }
        }
    }
    private void CommomUI(MeshAssetRule assetRule)
    {
        //using (new EditorGUI.DisabledScope(base.targets.Length > 1))
        //{
        //    assetRule.m_GlobalScale = EditorGUILayout.FloatField(styles.ScaleFactor, assetRule.m_GlobalScale,
        //        new GUILayoutOption[0]);
        //}

        //using (new EditorGUI.DisabledScope(true))
        //{
        //    assetRule.m_FileScale = EditorGUILayout.FloatField(styles.FileScaleFactor, assetRule.m_FileScale);
        //}

        assetRule.m_MeshCompression =
            (ModelImporterMeshCompression)
            EditorGUILayout.EnumPopup(styles.MeshCompressionLabel, assetRule.m_MeshCompression);
        assetRule.m_IsReadable        = EditorGUILayout.Toggle(styles.IsReadable, assetRule.m_IsReadable);
        assetRule.optimizeMeshForGPU  = EditorGUILayout.Toggle(styles.OptimizeMeshForGPU, assetRule.optimizeMeshForGPU);
        assetRule.m_ImportBlendShapes = EditorGUILayout.Toggle(styles.ImportBlendShapes, assetRule.m_ImportBlendShapes);
        assetRule.m_AddColliders      = EditorGUILayout.Toggle(styles.MeshCompressionLabel, assetRule.m_AddColliders);

        using (new EditorGUI.DisabledScope(true))
        {
            assetRule.keepQuads = EditorGUILayout.Toggle(styles.KeepQuads, assetRule.keepQuads);
        }
        assetRule.m_weldVertices      = EditorGUILayout.Toggle(styles.WeldVertices, assetRule.m_weldVertices);
        assetRule.swapUVChannels      = EditorGUILayout.Toggle(styles.SwapUVChannels, assetRule.swapUVChannels);
        assetRule.generateSecondaryUV = EditorGUILayout.Toggle(styles.GenerateSecondaryUV, assetRule.generateSecondaryUV);
        if (assetRule.generateSecondaryUV)
        {
            //EditorGUI.indentLevel++;
            //this.m_SecondaryUVAdvancedOptions = EditorGUILayout.Foldout(this.m_SecondaryUVAdvancedOptions,
            //    styles.GenerateSecondaryUVAdvanced, EditorStyles.foldout);
            //if (this.m_SecondaryUVAdvancedOptions)
            //{
            //    assetRule.secondaryUVHardAngle = EditorGUILayout.Slider(styles.secondaryUVHardAngle,
            //        assetRule.secondaryUVHardAngle, 0f, 180f, new GUILayoutOption[0]);
            //    assetRule.secondaryUVPackMargin = EditorGUILayout.Slider(styles.secondaryUVPackMargin,
            //        assetRule.secondaryUVPackMargin, 0f, 180f, new GUILayoutOption[0]);
            //    assetRule.secondaryUVAngleDistortion = EditorGUILayout.Slider(styles.secondaryUVAngleDistortion,
            //        assetRule.secondaryUVAngleDistortion, 0f, 180f, new GUILayoutOption[0]);
            //    assetRule.secondaryUVAreaDistortion = EditorGUILayout.Slider(styles.secondaryUVAreaDistortion,
            //        assetRule.secondaryUVAreaDistortion, 0f, 180f, new GUILayoutOption[0]);
            //}
            //EditorGUI.indentLevel--;
        }
    }
    /// <summary>
    /// inspector panel
    /// </summary>
    /// <param name="assetRule"></param>
    private void DrawMeshSettings(MeshAssetRule assetRule)
    {
        //GUILayout.Label(styles.Meshes, EditorStyles.boldLabel, new GUILayoutOption[0]);


        m_MeshFold = EditorGUILayout.Foldout(this.m_MeshFold,
                                             styles.GenerateMeshes, EditorStyles.foldout);

        if (m_MeshFold)
        {
            ModelPageGUI(assetRule);
            CommomUI(assetRule);
            NormalsAndTangentsGUI(assetRule);
            MaterialsGUI(assetRule);
        }

        m_RigFold = EditorGUILayout.Foldout(this.m_RigFold,
                                            styles.GenerateRigs, EditorStyles.foldout);

        if (m_RigFold)
        {
            assetRule.AnimationType = (ModelImporterAnimationType)
                                      EditorGUILayout.EnumPopup(styles.AnimationType, assetRule.AnimationType);

            if (assetRule.AnimationType == ModelImporterAnimationType.Generic ||
                assetRule.AnimationType == ModelImporterAnimationType.Human)
            {
                assetRule.isOptimizeObject = true;
            }
            else
            {
                assetRule.isOptimizeObject = false;
            }
        }

        m_AnimationsFold = EditorGUILayout.Foldout(this.m_AnimationsFold,
                                                   styles.GenerateAnimation, EditorStyles.foldout);

        if (m_AnimationsFold)
        {
            assetRule.ImportAnimation = EditorGUILayout.Toggle(styles.ImportAnimation, assetRule.ImportAnimation);
        }

        //CommomUI(assetRule);
        //NormalsAndTangentsGUI(assetRule);
        //MaterialsGUI(assetRule);
    }
    private void MaterialsGUI(MeshAssetRule rule)
    {
        GUILayout.Label(styles.Materials, EditorStyles.boldLabel, new GUILayoutOption[0]);
        rule.m_ImportMaterials = EditorGUILayout.Toggle(styles.ImportMaterials, rule.m_ImportMaterials);
        string text;

        if (rule.m_ImportMaterials)
        {
            rule.m_MaterialName   = (ModelImporterMaterialName)EditorGUILayout.EnumPopup(styles.MaterialName, rule.m_MaterialName);
            rule.m_MaterialSearch = (ModelImporterMaterialSearch)EditorGUILayout.EnumPopup(styles.MaterialSearch, rule.m_MaterialSearch);
            text = string.Concat(styles.MaterialHelpStart.text.Replace("%MAT%", styles.MaterialNameHelp[(int)rule.m_MaterialName].text), "\n", styles.MaterialSearchHelp[(int)rule.m_MaterialSearch].text, "\n", styles.MaterialHelpEnd.text);
        }
        else
        {
            text = styles.MaterialHelpDefault.text;
        }
        GUILayout.Label(new GUIContent(text), EditorStyles.helpBox, new GUILayoutOption[0]);
    }
 private void ModelPageGUI(MeshAssetRule assetRule)
 {
 }