需求分析
寵物預(yù)設(shè)上綁定腳本,需要填充很多數(shù)據(jù)贫贝,創(chuàng)建材質(zhì)球介粘,主要功能為:
1.填充Animaiton殖氏、材質(zhì)球、貼圖等數(shù)據(jù)
2.保存預(yù)設(shè)
3.極簡(jiǎn)碗短,并只在編輯器下有效
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(GamePetVisual),true)]
public class PetPrefabTool : UIWidgetContainerEditor
{
const string NormalPath = "Assets/IDS/Res/Models/Pet";
public string AnimationRootPath = "";
public Animation animddd;
public Texture MainTexture = null;
public override void OnInspectorGUI()
{
MainTexture = EditorGUILayout.ObjectField("添加主貼圖", MainTexture, typeof(Texture), true) as Texture;
if(MainTexture!=null)
{
if(string.IsNullOrEmpty(AnimationRootPath))
{
string mainTexturePath = AssetDatabase.GetAssetPath(MainTexture.GetInstanceID());
AnimationRootPath = mainTexturePath.Replace(MainTexture.name+".png","");
GamePetVisual gamePetVisual = (GamePetVisual)target;
gamePetVisual.MainTexture = MainTexture;
}
}
else
{
GamePetVisual gamePetVisual = (GamePetVisual)target;
if(gamePetVisual.MainTexture != null)
{
MainTexture = gamePetVisual.MainTexture;
}
}
AnimationRootPath = EditorGUILayout.TextField("動(dòng)畫文件路徑:", AnimationRootPath);
if (GUILayout.Button("一鍵填充"))
{
if(string.IsNullOrEmpty(AnimationRootPath))
{
EditorUtility.DisplayDialog("錯(cuò)誤", "動(dòng)畫文件路徑不能為空,必須先指定路徑受葛!", "是");
return;
}
if (MainTexture == null)
{
EditorUtility.DisplayDialog("錯(cuò)誤", "主貼圖名不能為空,必須先設(shè)置!例如:xiaoshumiao", "是");
return;
}
GamePetVisual gamePetVisual = (GamePetVisual)target;
setPetAimiation(gamePetVisual);
setGamePetVisual(gamePetVisual);
NGUITools.SetLayer(gamePetVisual.gameObject, LayerMask.NameToLayer("Dude"));
gamePetVisual.MainTexture = MainTexture;
EditorUtility.SetDirty(gamePetVisual.gameObject);
AssetDatabase.SaveAssets();
}
base.OnInspectorGUI();
}
void setPetAimiation(GamePetVisual gamePetVisual)
{
string[] animsName = {"deathRole","idle","flightIdle","glide","flight"};
List<AnimationClip> clips = new List<AnimationClip>();
for (int i = 0; i < animsName.Length; i++)
{
string animPath = AnimationRootPath + animsName[i]+".anim";
AnimationClip animClip = AssetDatabase.LoadAssetAtPath<AnimationClip>(animPath);
if(animClip!=null)
{
clips.Add(animClip);
Debug.Log("Find Clips :" + animPath);
}
}
if(clips.Count>0)
{
Animation myAnimation = gamePetVisual.gameObject.AddMissingComponent<Animation>();
AnimationUtility.SetAnimationClips(myAnimation,clips.ToArray());
myAnimation.playAutomatically = true;
myAnimation.animatePhysics = false;
myAnimation.cullingType = AnimationCullingType.BasedOnRenderers;
myAnimation.clip = clips[0];
}
}
void setGamePetVisual(GamePetVisual gamePetVisual)
{
gamePetVisual.animatorType = AnimatorType.Eagle;
SkinnedMeshRenderer render = gamePetVisual.GetComponentInChildren<SkinnedMeshRenderer>();
if(render!=null)
{
gamePetVisual.meshRenderer = render;
}
string transMaterialPaht = string.Format("{0}Materials/{1}_transparent.mat", AnimationRootPath, MainTexture.name);
Material transMaterial = AssetDatabase.LoadAssetAtPath<Material>(transMaterialPaht);
if(transMaterial==null)
{
//創(chuàng)建一個(gè)新材質(zhì)球
Shader shader = Shader.Find("TempleRun2/Additive");
if(shader==null)
{
EditorUtility.DisplayDialog("錯(cuò)誤", "Shader TempleRun2/Additive is null,檢查為什么偎谁!", "是");
return;
}
transMaterial = new Material(shader);
transMaterial.color = Color.white;
AssetDatabase.CreateAsset(transMaterial, transMaterialPaht);
}
transMaterial = AssetDatabase.LoadAssetAtPath<Material>(transMaterialPaht);
if(transMaterial!=null)
{
if (MainTexture != null)
{
transMaterial.mainTexture = MainTexture;
}
else
{
Debug.LogError("MainTexture is null !");
}
gamePetVisual.transparentMaterial = transMaterial;
}
}
}