Created
August 3, 2015 06:10
-
-
Save whaison/428605abe0d950302748 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using UnityEditor.Animations; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Text.RegularExpressions; | |
using System.Linq; | |
/// <summary> | |
/// Model animation spliter. | |
/// by Koki Ibukuro @asus4 >Noboru Otsuka | |
/// </summary> | |
public class ModelAnimationSplitterWithAnimeController : EditorWindow { | |
public TextAsset csvAsset; | |
public GameObject modelAsset; | |
string modelAssetPath; | |
string CharaName; | |
string AnimationNameStr; | |
string AnimationClipEventNameStr; | |
string CharaDir; | |
int build_colWidth; | |
int build_colWidthleft; | |
int build_colWidthright; | |
int build_rowHeight; | |
float buildX; | |
float buildY; | |
float buildY_event; | |
bool build_baseLayer_start; | |
bool build_left_start; | |
bool build_right_start; | |
bool build_left_now; | |
bool build_right_now; | |
int build_count; | |
string sd_num_charaname; | |
string sd_num; | |
bool bool_effect_fix; | |
string fix_anim_09_attack_event_anim_path; | |
string fix_anim_10_skill_event_anim_path; | |
AnimationClip fix_anim_09_attack_event_anim_ref; | |
AnimationClip fix_anim_10_skill_event_anim_ref; | |
//Vector3 build_startPosition=new Vector3(-build_colWidth,-100,0); | |
UnityEditor.Animations.AnimatorController AnimatorControllerRef; | |
AnimatorStateMachine BaseLayerStateMachine; | |
AnimatorStateMachine EventLayerStateMachine; | |
private List<UnityEditor.Animations.AnimatorState> BaseLayerStateList = new List<UnityEditor.Animations.AnimatorState>(); | |
private List<UnityEditor.Animations.AnimatorState> EventLayerStateList = new List<UnityEditor.Animations.AnimatorState>(); | |
List<AnimationClip> CharaAnimationClipListRef; | |
List<AnimationClip> eventAnimationClipList= new List<AnimationClip>(); | |
void OnGUI () | |
{ | |
GUILayout.Label ("CSV to Split Animtion V1.0.3", EditorStyles.boldLabel); | |
GUILayout.Space (10f); | |
csvAsset = EditorGUILayout.ObjectField ("CSV Text", csvAsset, typeof(TextAsset), false) as TextAsset; | |
if (csvAsset == null) { | |
GUILayout.Label ("Set CSV Data. Extension need .txt"); | |
} | |
GUILayout.Space (10f); | |
modelAsset = EditorGUILayout.ObjectField ("3D Model", modelAsset, typeof(GameObject), false) as GameObject; | |
if (modelAsset == null) { | |
GUILayout.Label ("Set FBX or other 3D model object."); | |
} | |
GUILayout.Space (20f); | |
if (GUILayout.Button ("Split with CSV", GUILayout.Width (140f))) { | |
SplitWithCsv (); | |
} | |
} | |
void build_Reset() | |
{ | |
modelAssetPath=""; | |
CharaName=""; | |
AnimationNameStr=""; | |
AnimationClipEventNameStr=""; | |
CharaDir=""; | |
build_colWidth=250; | |
build_colWidthleft = 250; | |
build_colWidthright = 200; | |
build_rowHeight=50; | |
buildX=0; | |
buildY=0; | |
buildY_event = 0; | |
build_baseLayer_start=false; | |
build_left_start=false; | |
build_right_start=false; | |
build_left_now=false; | |
build_right_now=false; | |
build_count=1; | |
bool_effect_fix = false; | |
string fix_anim_09_attack_event_anim_path = ""; | |
string fix_anim_10_skill_event_anim_path = ""; | |
AnimationClip fix_anim_09_attack_event_anim_ref = null; | |
AnimationClip fix_anim_10_skill_event_anim_ref = null; | |
} | |
public void SplitWithCsv () | |
{ | |
///////////////////////////////////// | |
build_Reset (); | |
/////////////////////////////////////// | |
if (Assert (csvAsset != null, "Need to set CSV Text") || Assert (modelAsset != null, "Need to set 3D Model")) { | |
return; | |
} | |
string csv = csvAsset.text; | |
Debug.Log ("csvAsset=" + csvAsset); | |
Debug.Log ("modelAsset=" + modelAsset); | |
/**/ | |
///////////////////////////////////////////////////////////////////////////// | |
string modelDirStr = AssetDatabase.GetAssetPath (modelAsset); | |
Debug.Log ("modelDirStr=" + modelDirStr); | |
string[] modelDirArr; | |
modelDirArr=modelDirStr.Split("/"[0]); | |
string buildDir = modelDirArr[0]; | |
for(int i = 1; i < modelDirArr.Length-1; i++) | |
{ | |
buildDir = buildDir +"/"+ modelDirArr [i]; | |
} | |
CharaDir = buildDir; | |
Debug.Log ("CharaDir="+CharaDir); | |
CharaName = modelDirArr [modelDirArr.Length-1]; | |
Debug.Log ("CharaName="+CharaName); | |
string[] CharaNamaArr; | |
CharaNamaArr=CharaName.Split("_"[0]); | |
sd_num_charaname = CharaNamaArr [0] + "_" + CharaNamaArr [1] + "_" + CharaNamaArr [2]; | |
sd_num = CharaNamaArr [0] + "_" + CharaNamaArr [1]; | |
/////////////////////////////////////////////////////////////////////////////// | |
//bool_effect_fix | |
//CharaDir | |
string EditedAnimation_fix_path=CharaDir+"/EditedAnimation_fix"; | |
Debug.Log ("EditedAnimation_fix_path= "+EditedAnimation_fix_path); | |
if (File.Exists (EditedAnimation_fix_path+"/anim_09_attack_event.anim")) { | |
Debug.Log ("すでにFiXしたエフェクト用アニメーションが見つかりました!" + EditedAnimation_fix_path); | |
fix_anim_09_attack_event_anim_path = EditedAnimation_fix_path + "/anim_09_attack_event.anim"; | |
fix_anim_10_skill_event_anim_path = EditedAnimation_fix_path + "/anim_10_skill_event.anim"; | |
fix_anim_09_attack_event_anim_ref = AssetDatabase.LoadAssetAtPath (fix_anim_09_attack_event_anim_path,typeof(AnimationClip)) as AnimationClip; | |
fix_anim_10_skill_event_anim_ref = AssetDatabase.LoadAssetAtPath (fix_anim_10_skill_event_anim_path,typeof(AnimationClip)) as AnimationClip; | |
Debug.Log ("#########fix_anim_09_attack_event_anim_ref="+fix_anim_09_attack_event_anim_ref); | |
Debug.Log ("##########fix_anim_10_skill_event_anim_ref="+fix_anim_10_skill_event_anim_ref); | |
bool_effect_fix = true; | |
} else { | |
Debug.Log ("エフェクト用アニメーションないので作ります。" + EditedAnimation_fix_path); | |
} | |
//////////////////////////////////////////////////////////////////////////////// | |
string ACpath; | |
ACpath=CharaDir+"/AnimatorController_"+sd_num_charaname+".controller"; | |
UnityEditor.Animations.AnimatorController controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(ACpath); | |
AnimatorControllerRef = controller; | |
AnimatorControllerRef.RemoveLayer(0); | |
AnimatorControllerRef.AddLayer("BaseLayer"); | |
BaseLayerStateMachine = AnimatorControllerRef.layers [0].stateMachine; | |
BaseLayerStateMachine.exitPosition = new Vector3 (0,-150,0); | |
BaseLayerStateMachine.entryPosition = new Vector3 (0,-100,0); | |
BaseLayerStateMachine.anyStatePosition = new Vector3 (0,100,0); | |
AnimatorControllerRef.AddLayer("EventLayer"); | |
EventLayerStateMachine = AnimatorControllerRef.layers [1].stateMachine; | |
EventLayerStateMachine.exitPosition = new Vector3 (0,-150,0); | |
EventLayerStateMachine.entryPosition = new Vector3 (0,-100,0); | |
EventLayerStateMachine.anyStatePosition = new Vector3 (0,100,0); | |
EventLayerStateMachine.AddState ("Default State", new Vector3 (-250, -100,0)); | |
////////////////////// | |
//build_Reset (); | |
///////////////////// | |
//////////////////////////////////////////////////////////////////////////////// | |
ModelImporterClipAnimation[] clipAnimations = ParceCSV (csv); | |
Debug.Log ("SplitWithCsv () clipAnimations= "+clipAnimations); | |
//AnimationClip AnimationClipRef; | |
CreateAnimationClip(CharaNamaArr[2]); | |
UnityEditor.Animations.AnimatorState state001= BaseLayerStateMachine.AddState(CharaNamaArr [2],new Vector3(0,buildY,0)); | |
state001.motion = AnimationClipRef; | |
UnityEditor.Animations.AnimatorState state002 = EventLayerStateMachine.AddState(CharaNamaArr [2],new Vector3(0,buildY,0)); | |
state002.motion = AnimationClipRef; | |
/* | |
if (Assert (clipAnimations != null, "can't parse CSV to Animation")) { | |
Debug.Log (csv); | |
return; | |
} | |
*/ | |
string path = AssetDatabase.GetAssetPath (modelAsset); | |
modelAssetPath = path; | |
ModelImporter importer = AssetImporter.GetAtPath (path) as ModelImporter; | |
if (Assert (importer != null, "set 3D model, like FBX,etc..")) { | |
return; | |
} | |
//importer.splitAnimations = true; // obsolute in Unity4 | |
importer.clipAnimations = clipAnimations; | |
AssetDatabase.ImportAsset (path); | |
//DisplayDialog ("Success", "finish split!"); | |
FbxSeachAndStateMotionSettiong (); | |
EditedAnimationStateMotionSetting (); | |
CreateFbxToPrefab (); | |
FbxPefabSetting (); | |
Close (); | |
DisplayDialog ("Success", "finish split! And AnimatorController State.Motion Attach clips!"); | |
if (bool_effect_fix == true) { | |
string AnimationEvent_normal_path=CharaDir+"/AnimationEvent"; | |
string EditedAnimation_normal_path=CharaDir+"/EditedAnimation"; | |
AssetDatabase.DeleteAsset (AnimationEvent_normal_path); | |
AssetDatabase.DeleteAsset (EditedAnimation_normal_path); | |
AssetDatabase.Refresh (); | |
} | |
} | |
void FbxSeachAndStateMotionSettiong(){ | |
//fbxファイルからAnimationClipを取得する | |
//http://qiita.com/fujimisakari/items/dfcc1230cd135cf134a1 | |
//CharaAnimationClipListRef=GetAnimationClipInFbx (modelAsset.name); | |
Object[] all = AssetDatabase.LoadAllAssetsAtPath(modelAssetPath); | |
foreach (var item in all) | |
{ | |
var clip = item as AnimationClip; | |
if (clip != null) | |
{ | |
Debug.Log ("clip : " + item.name); | |
string animationName = item.name; | |
string[] arr = animationName.Split ('_'); | |
if (arr[0]=="anim") { | |
int animNum=int.Parse (arr[1]); | |
BaseLayerStateList [animNum - 1].motion = item as UnityEngine.Motion; | |
Debug.Log ("BaseLayerStateList ["+animNum+ "- 1].name"+BaseLayerStateList [animNum - 1].name); | |
Debug.Log ("BaseLayerStateList ["+animNum+ "- 1].motion"+BaseLayerStateList [animNum - 1].motion); | |
} | |
} | |
} | |
} | |
void EditedAnimationStateMotionSetting(){ | |
string EditedAnimationPath = CharaDir + "/" + "EditedAnimation"; | |
//Debug.Log ("------------EditedAnimationPath= "+EditedAnimationPath); | |
///Object[] all = AssetDatabase.LoadAllAssetsAtPath(EditedAnimationPath); | |
//foreach (var item in ) | |
for(int i = 0; i < eventAnimationClipList.Count; i++) | |
{ | |
//Debug.Log("Creating enemy number: " + i); | |
var clip = eventAnimationClipList[i] as AnimationClip; | |
if (clip != null) | |
{ | |
Debug.Log ("-----eventAnimationClipList-----clip : " + clip.name); | |
string animationName = clip.name; | |
string[] arr = animationName.Split ('_'); | |
if (arr[0]=="anim") { | |
int animNum=int.Parse (arr[1]); | |
EventLayerStateList [i].motion = clip as UnityEngine.Motion; | |
Debug.Log ("---------EventLayerStateList ["+i+ "].name = "+EventLayerStateList [i].name); | |
Debug.Log ("---------EventLayerStateList ["+i+ "].motion = "+EventLayerStateList [i].motion); | |
if (bool_effect_fix==true) { | |
if (EventLayerStateList [i].name == "anim_09_attack") { | |
EventLayerStateList [i].motion = fix_anim_09_attack_event_anim_ref; | |
} | |
if (EventLayerStateList [i].name == "anim_10_skill") { | |
EventLayerStateList [i].motion = fix_anim_10_skill_event_anim_ref; | |
} | |
} | |
Debug.Log ("-----FIXなので参照かえます----EventLayerStateList ["+i+ "].name = "+EventLayerStateList [i].name); | |
Debug.Log ("-----FIXなので参照かえます------EventLayerStateList ["+i+ "].motion = "+EventLayerStateList [i].motion); | |
} | |
} | |
} | |
} | |
GameObject fbxPrefabRef; | |
//string sd_num_charaname; | |
void CreateFbxToPrefab () | |
{ | |
var go = Selection.activeGameObject; | |
go = modelAsset; | |
//Mesh m1 = go.GetComponent<MeshFilter>().mesh;//update line1 | |
//AssetDatabase.CreateAsset(m1, "Assets/savedMesh/" + go.name +"_M" + ".asset"); // update line2 | |
string modelDirStr = AssetDatabase.GetAssetPath (go); | |
Debug.Log ("modelDirStr=" + modelDirStr); | |
string[] modelDirArr; | |
modelDirArr=modelDirStr.Split("/"[0]); | |
string buildDir = modelDirArr[0]; | |
for(int i = 1; i < modelDirArr.Length-1; i++) | |
{ | |
buildDir = buildDir +"/"+ modelDirArr [i]; | |
} | |
string CharaDir = buildDir; | |
Debug.Log ("CharaDir="+CharaDir); | |
string CharaName = modelDirArr [modelDirArr.Length-1]; | |
Debug.Log ("CharaName="+CharaName); | |
var prefab = EditorUtility.CreateEmptyPrefab(CharaDir +"/"+ sd_num+ "_prefab.prefab"); | |
fbxPrefabRef=EditorUtility.ReplacePrefab(go, prefab); | |
AssetDatabase.Refresh(); | |
//////////////////////////// | |
//fbxPrefabRef = prefab as GameObject; | |
/////////////////////////// | |
string path = AssetDatabase.GetAssetPath (fbxPrefabRef); | |
AssetDatabase.ImportAsset (path); | |
} | |
SkinnedMeshRenderer SkinnedMeshRendererRef; | |
Material FbxPrefabMeshMaterialRef; | |
GameObject FbxPrefabMeshRef; | |
Texture2D FbxPrefabTexture2D; | |
Texture FbxPrefabTexture; | |
public Shader shader1TransparentCutout; | |
public Shader shader2TransparentVegetation; | |
public Shader shader3TransparentVegetationGradation; | |
void FbxPefabSetting(){ | |
//fbx_prefab_ref.GetComponent<Animator>().runtimeAnimatorController= | |
Debug.Log ("------fbxPrefabRef= " + fbxPrefabRef); | |
Animator animator = fbxPrefabRef.GetComponent<Animator>(); | |
Debug.Log ("------fbxPrefabRef animator= " + animator); | |
Debug.Log ("------fbxPrefabRef animator.runtimeAnimatorController= "+animator.runtimeAnimatorController); | |
Debug.Log ("------AnimatorControllerRef= "+AnimatorControllerRef); | |
animator.runtimeAnimatorController = (RuntimeAnimatorController)AnimatorControllerRef as RuntimeAnimatorController; | |
//animator.runtimeAnimatorController = (RuntimeAnimatorController)RuntimeAnimatorController.Instantiate(Resources.Load ("path/to/controller_file")); | |
fbxPrefabRef.transform.localPosition = new Vector3 (0,0,0); | |
fbxPrefabRef.transform.localRotation = Quaternion.Euler (new Vector3 (0, 0, 0)); | |
fbxPrefabRef.transform.localScale = new Vector3 (1.0f, 1.0f, 0.1f); | |
fbxPrefabRef.AddComponent<IdleMotionChangerTrigger> (); | |
fbxPrefabRef.AddComponent<AnimationClipEventHandler> (); | |
//fbxPrefabRefに子供いたら | |
int countp1 = 0; | |
foreach (Transform child in fbxPrefabRef.transform) { | |
Debug.Log ("Child[" + countp1 + "]:" + child.name); | |
string[] CharaNamaArr; | |
CharaNamaArr=child.name.Split("_"[0]); | |
if (CharaNamaArr [0] == "m2dtoolkit") | |
{ | |
FbxPrefabMeshRef=child.gameObject; | |
Debug.Log ("-------メッシュが見つかりました。!!!!!FbxPrefabMeshRef="+FbxPrefabMeshRef.name); | |
}else | |
{ | |
if (FbxPrefabMeshRef == null) | |
{ | |
Debug.Log ("-------メッシュが見つかりませんでした"); | |
} | |
} | |
countp1++; | |
} | |
SkinnedMeshRendererRef=FbxPrefabMeshRef.GetComponent<SkinnedMeshRenderer> (); | |
//FbxPrefabMeshMaterialRef=SkinnedMeshRendererRef.materials [0]; | |
FbxPrefabMeshMaterialRef = SkinnedMeshRendererRef.sharedMaterial; | |
FbxPrefabMeshMaterialRef.name = sd_num_charaname + "_png.png"; | |
//Debug.Log ("---------FbxPrefabMeshMaterialRef.name=" + FbxPrefabMeshMaterialRef.name); | |
Debug.Log ("---------FbxPrefabMeshMaterialRef.name="+FbxPrefabMeshMaterialRef.name); | |
shader1TransparentCutout = Shader.Find("Unlit/Transparent Cutout"); | |
shader2TransparentVegetation = Shader.Find("Unlit/Transparent Vegetation"); | |
shader3TransparentVegetationGradation = Shader.Find ("Unlit/Transparent VegetationGradation"); | |
//shader3TransparentVegetationGradation = AssetDatabase.LoadAssetAtPath("/Assets/SD_Chara/shader/"+ "Transparent VegetationGradation.shader", typeof(Shader)) as Shader; | |
//shader2TransparentVegetation. | |
FbxPrefabMeshMaterialRef.shader = shader3TransparentVegetationGradation; | |
//Texture2D FbxPrefabTexture2D = AssetDatabase.LoadAssetAtPath(CharaDir +"/"+ sd_num_charaname+ "_png.png", Texture2D) as Texture2D; | |
Texture FbxPrefabTexture = AssetDatabase.LoadAssetAtPath(CharaDir +"/"+ sd_num_charaname+ "_png.png", typeof(Texture)) as Texture; | |
//FbxPrefabTexture2D. | |
FbxPrefabMeshMaterialRef.SetTexture("_MainTex",FbxPrefabTexture); | |
} | |
bool Assert (bool b, string error) | |
{ | |
if (!b) { | |
DisplayDialog ("Warning", error); | |
return true; | |
} else { | |
return false; | |
} | |
} | |
void DisplayDialog (string title, string message) | |
{ | |
Debug.LogWarning (message); | |
EditorUtility.DisplayDialog (title, message, "OK"); | |
} | |
ModelImporterClipAnimation[] ParceCSV (string csv) | |
{ | |
// split to lines | |
string[] lines = csv.Split ('\n'); | |
//Debug.Log("[]lines = "+ []lines); | |
char[] trims = {'\r', ' '}; | |
Debug.Log("trims = "+trims); | |
List<ModelImporterClipAnimation> clips = new List<ModelImporterClipAnimation> (); | |
Debug.Log("lines.Length; = "+lines.Length); | |
for (int i=0; i<lines.Length; i++) | |
{ | |
string s = lines [i].Trim (trims); | |
ModelImporterClipAnimation clip = ParseClip (s); | |
Debug.Log ("ParseClip(c) の戻り値=clip= "+clip); | |
if (clip != null) { | |
Debug.Log ("clipsにAdd する clip= "+clip); | |
clips.Add (clip); | |
} | |
} | |
if (clips.Count == 0) { | |
Debug.Log ("CSVのクリップスは clips.Count= 0 個ありました"); | |
return null; | |
} else { | |
Debug.Log ("CSVのクリップスは clips.Count= " + clips.Count + " 個ありました"); | |
build_count = 0; | |
} | |
return clips.ToArray(); | |
} | |
ModelImporterClipAnimation ParseClip (string line) | |
{ | |
string[] arr = line.Split (','); | |
//Debug.Log("[] arr = "+[] arr); | |
Debug.Log("arr.Length = "+arr.Length); | |
if (arr.Length < 4) { | |
return null; | |
} else { | |
Debug.Log ("CSV行の長さは必要なだけあります。"); | |
} | |
ModelImporterClipAnimation clip = new ModelImporterClipAnimation (); | |
// name | |
string name = arr [0]; | |
if (!string.IsNullOrEmpty (name)) { | |
clip.name = name; | |
Debug.Log ("clip.name =" + clip.name); | |
AnimationNameStr = clip.name; | |
AnimatorControllerRef.AddParameter(AnimationNameStr,UnityEngine.AnimatorControllerParameterType.Trigger); | |
} else { | |
return null; | |
} | |
// first | |
try { | |
//clip.firstFrame = int.Parse (arr [1]); // change to float in unity4 | |
clip.firstFrame = float.Parse (arr [1]); | |
Debug.Log ("clip.firstFrame =" + clip.firstFrame); | |
} catch { | |
// | |
return null; | |
} | |
// last | |
try { | |
//clip.lastFrame = int.Parse (arr [2]); // change to float in unity4 | |
clip.lastFrame = float.Parse (arr [2])-1; | |
Debug.Log ("clip.lastFrame =" + clip.lastFrame); | |
/////////////////////////////////////////// | |
/// | |
/// | |
/// | |
if(build_baseLayer_start==false){ | |
Debug.Log("------build_baseLayer_start="+build_baseLayer_start+" ここはfalseでスタート-----buildY = "+buildY+"-----build_count="+build_count+"-------"); | |
build_baseLayer_start=true; | |
build_left_start=true; | |
} | |
if(build_left_start==true) | |
{ | |
buildY=-100.0f; | |
Debug.Log("------build_left_start="+build_left_start+" ここはtrueでスタート-----buildY = "+buildY+"----build_count="+build_count+"-----AnimationNameStr="+AnimationNameStr+"-----"); | |
build_left_start=false; | |
build_left_now=true; | |
} | |
if(build_left_now==true){ | |
Debug.Log("------build_left_now="+build_left_now+" ここはtrueでadd--buildY = "+buildY+"----build_count="+build_count+"--AnimationNameStr="+AnimationNameStr+"--add---"); | |
if(build_count<=11){ | |
UnityEditor.Animations.AnimatorState state001 = BaseLayerStateMachine.AddState(clip.name,new Vector3(-build_colWidthleft,buildY,0)); | |
UnityEditor.Animations.AnimatorStateTransition state001Transition = BaseLayerStateMachine.AddAnyStateTransition(state001); | |
state001Transition.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, AnimationNameStr); | |
if(AnimationNameStr=="anim_01_wait"){ | |
state001.writeDefaultValues=true; | |
Debug.Log("-----------------anim_01_wait のとき state001.writeDefaultValues= "+state001.writeDefaultValues); | |
} | |
BaseLayerStateList.Add(state001); | |
//state001.motion=clip. as UnityEngine.Motion; | |
buildY=buildY+build_rowHeight*1.0f; | |
build_count++; | |
} | |
} | |
Debug.Log("----------build_right_start="+build_right_start+" ここはtrueでスタート-------どう?---AnimationNameStr="+AnimationNameStr+"----build_right_start"); | |
if(build_right_start==true) | |
{ | |
buildY=-100.0f; | |
Debug.Log("------build_right_start="+build_right_start+" ここはtrueでスタート-----buildY = "+buildY+"----build_count="+build_count+"-----AnimationNameStr="+AnimationNameStr+"-----"); | |
build_right_start=false; | |
build_right_now=true; | |
} | |
if(build_right_now==true){ | |
Debug.Log("------build_right_now="+build_right_now+" ここはtrueでadd---buildY = "+buildY+"----build_count="+build_count+"---AnimationNameStr="+AnimationNameStr+"-add---"); | |
if(build_count<=22){ | |
UnityEditor.Animations.AnimatorState state002 = BaseLayerStateMachine.AddState(clip.name,new Vector3(build_colWidthright,buildY,0)); | |
UnityEditor.Animations.AnimatorStateTransition state002Transition = BaseLayerStateMachine.AddAnyStateTransition(state002); | |
state002Transition.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, AnimationNameStr); | |
BaseLayerStateList.Add(state002); | |
buildY=buildY+build_rowHeight*1.0f; | |
build_count++; | |
} | |
} | |
if(build_count==11){ | |
build_left_start=false; | |
build_left_now=false; | |
build_right_start=true; | |
build_right_now=false; | |
} | |
if(build_count==22){ | |
// | |
build_left_start=false; | |
build_left_now=false; | |
build_right_start=false; | |
build_right_now=false; | |
} | |
///////////////////////////////////////////// | |
} catch { | |
return null; | |
} | |
try { | |
//clip.lastFrame = int.Parse (arr [2]); // change to float in unity4 | |
bool myLoopTimebool=true; | |
switch (AnimationNameStr) | |
{ | |
case "anim_01_wait": | |
myLoopTimebool=true; | |
break; | |
case "anim_02_run": | |
myLoopTimebool=true; | |
break; | |
case "anim_03_stance": | |
myLoopTimebool=true; | |
break; | |
case "anim_04_block": | |
myLoopTimebool=false; | |
break; | |
case "anim_05_damage": | |
myLoopTimebool=false; | |
break; | |
case "anim_06_near_death": | |
myLoopTimebool=true; | |
break; | |
case "anim_07_death": | |
myLoopTimebool=false; | |
break; | |
case "anim_08_victory": | |
myLoopTimebool=true; | |
break; | |
case "anim_09_attack": | |
myLoopTimebool=false; | |
break; | |
case "anim_10_skill": | |
myLoopTimebool=false; | |
break; | |
case "anim_11_state_poison": | |
myLoopTimebool=true; | |
break; | |
case "anim_12_state_paralysis": | |
myLoopTimebool=true; | |
break; | |
case "anim_13_state_damaged": | |
myLoopTimebool=true; | |
break; | |
case "anim_14_state_delicate": | |
myLoopTimebool=true; | |
break; | |
case "anim_15_state_sickness": | |
myLoopTimebool=true; | |
break; | |
case "anim_16_state_damned": | |
myLoopTimebool=true; | |
break; | |
case "anim_17_state_confusion": | |
myLoopTimebool=true; | |
break; | |
case "anim_18_state_berserk": | |
myLoopTimebool=true; | |
break; | |
case "anim_19_state_transform": | |
myLoopTimebool=true; | |
break; | |
case "anim_20_state_minimum": | |
myLoopTimebool=true; | |
break; | |
case "anim_21_state_near_death": | |
myLoopTimebool=true; | |
break; | |
default: | |
myLoopTimebool=true; | |
break; | |
} | |
clip.loopTime=myLoopTimebool; | |
Debug.Log ("clip.loopTime =" + clip.loopTime); | |
} catch { | |
return null; | |
} | |
/////////////////////////////Effect | |
/// | |
/// | |
List<AnimationEvent> animEventsList1 = new List<AnimationEvent>(); | |
try { | |
//////////////////////// | |
/// //////////////////////////// | |
string eventStr1 = arr [3]; | |
// アニメーションイベントの作成 | |
AnimationEvent animEvent1 = new AnimationEvent(); | |
//AnimationEvent[] animEvents; | |
// 関数名をセット | |
animEvent1.functionName =eventStr1; | |
AnimationClipEventNameStr=animEvent1.functionName; | |
// Int型のパラメーターを格納 | |
animEvent1.intParameter = 0; | |
//animEvent.time=arr [4]; | |
//animEvent.objectReferenceParameter=objEffect_0_start as UnityEngine.Object ; | |
// 設定した時間にイベントを送信 | |
animEvent1.time = float.Parse (arr [4]); | |
// アニメーションイベントの追加 | |
//clip.AddEvent(animEvent); | |
if(arr [3+1]!=null){ | |
Debug.Log("while loop arr["+3+"+1]="+arr [3+1]+" があるので実行"); | |
////////////////////////////////////// | |
//AnimationClipRef; | |
CreateAnimationClip(AnimationNameStr+"_event"); | |
Crate(); | |
/////////////////////////////////////// | |
animEvent1.objectReferenceParameter=ScriptableObjectRef as UnityEngine.Object ; | |
// アニメーションイベントの追加 | |
//clip.AddEvent(animEvent); | |
animEventsList1.Add(animEvent1); | |
} | |
//animEventsList1.Add(animEvent1); | |
} catch { | |
//return null; | |
} | |
try { | |
//////////////////////// | |
/// //////////////////////////// | |
string eventStr2= arr [5]; | |
// アニメーションイベントの作成 | |
AnimationEvent animEvent2 = new AnimationEvent(); | |
//AnimationEvent[] animEvents; | |
// 関数名をセット | |
animEvent2.functionName =eventStr2; | |
AnimationClipEventNameStr=animEvent2.functionName; | |
// Int型のパラメーターを格納 | |
animEvent2.intParameter = 0; | |
//animEvent.time=arr [4]; | |
//animEvent.objectReferenceParameter=objEffect_0_start as UnityEngine.Object ; | |
// 設定した時間にイベントを送信 | |
animEvent2.time = float.Parse (arr [6]); | |
// アニメーションイベントの追加eventAnimationClipList | |
//clip.AddEvent(animEvent); | |
if(arr [5+1]!=null){ | |
Debug.Log("while loop arr["+5+"+1]="+arr [5+1]+" があるので実行"); | |
////////////////////////////////////// | |
Crate(); | |
/////////////////////////////////////// | |
animEvent2.objectReferenceParameter=ScriptableObjectRef as UnityEngine.Object ; | |
// アニメーションイベントの追加 | |
//clip.AddEvent(animEvent); | |
animEventsList1.Add(animEvent2); | |
} | |
//animEventsList1.Add(animEvent2); | |
} catch { | |
//return null; | |
} | |
try{ | |
//for(int i = 7; i < arr.Length; i+2) | |
//{ | |
int i=7; | |
while(i < arr.Length) | |
{ | |
//Debug.Log ("I've washed a cup!"); | |
Debug.Log("--------while loop i="+i+"----------------------"); | |
/// //////////////////////////// | |
string eventStr3= arr [i]; | |
Debug.Log("while loop eventStr3="+eventStr3); | |
// アニメーションイベントの作成 | |
AnimationEvent animEvent3 = new AnimationEvent(); | |
//AnimationEvent[] animEvents; | |
// 関数名をセット | |
animEvent3.functionName =eventStr3; | |
AnimationClipEventNameStr=animEvent3.functionName; | |
// Int型のパラメーターを格納 | |
animEvent3.intParameter = 0; | |
//animEvent.time=arr [4]; | |
// 設定した時間にイベントを送信 | |
animEvent3.time = float.Parse (arr [i+1]); | |
Debug.Log("while loop animEvent3.time="+animEvent3.time); | |
if(arr [i+1]!=null){ | |
Debug.Log("while loop arr["+i+"+1]="+arr [i+1]+" があるので実行"); | |
////////////////////////////////////// | |
Crate(); | |
/////////////////////////////////////// | |
animEvent3.objectReferenceParameter=ScriptableObjectRef as UnityEngine.Object ; | |
// アニメーションイベントの追加 | |
//clip.AddEvent(animEvent); | |
animEventsList1.Add(animEvent3); | |
} | |
Debug.Log("--------while loop i="+i+"----------------------end"); | |
i=i+2; | |
Debug.Log("--------while loop i="+i+"--------------------next"); | |
} | |
} catch { | |
//return null; | |
} | |
if(animEventsList1.Count!=0){ | |
AnimationEvent[] animationEventArr1 = animEventsList1.ToArray(); | |
//clip.events = animationEventArr1; | |
AnimationClipRef.frameRate = 24; | |
//AnimationClipRef. | |
//AnimationClipRef.events= animationEventArr1; | |
AnimationUtility.SetAnimationEvents(AnimationClipRef, animationEventArr1); | |
eventAnimationClipList.Add (AnimationClipRef); | |
//public static void SetAnimationEvents(AnimationClip clip, AnimationEvent[] events); | |
//Debug.Log ("clip.events =" + clip.events); | |
Debug.Log ("AnimationClipRef.events =" + AnimationClipRef.events); | |
/////////////////////////////////////////////////////////////////////////// | |
if (buildY_event == 0) { | |
buildY_event = buildY; | |
} else { | |
buildY_event = buildY_event + build_rowHeight; | |
} | |
UnityEditor.Animations.AnimatorState state003 = EventLayerStateMachine.AddState(clip.name,new Vector3(-build_colWidthleft,buildY_event,0)); | |
UnityEditor.Animations.AnimatorStateTransition state003Transition = EventLayerStateMachine.AddAnyStateTransition(state003); | |
state003Transition.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, AnimationNameStr); | |
EventLayerStateList.Add(state003); | |
if (buildY_event == 0) { | |
} | |
/// /////////////////////////////////////////////////////////////////////// | |
} | |
/* | |
// wrapmode | |
try { | |
clip.wrapMode = (WrapMode) System.Enum.Parse(typeof(WrapMode), arr [3]); | |
} catch { | |
return null; | |
} | |
if (clip.wrapMode == WrapMode.Loop || clip.wrapMode == WrapMode.PingPong) { | |
clip.loop = true; | |
} | |
*/ | |
Debug.Log ("ParseClipの返し値=clip=" + clip); | |
return clip; | |
} | |
/// <summary> | |
/// CreateScriptableObjectPrefub | |
/// </summary> | |
/// | |
string[] labels = {"Data", "ScriptableObject"}; | |
ScriptableObject ScriptableObjectRef; | |
void Crate () | |
{ | |
Debug.Log("--------Crate ----------------------"); | |
Debug.Log("--Crate-- Selection.objects="+Selection.objects); | |
//foreach (Object selectedObject in Selection.objects) { | |
//Object modelAssetObject=(modelAsset)Object; | |
//selectedObject = | |
//selectedObject c = obj as TestClass; | |
//Debug.Log("Crate selectedObject="+selectedObject); | |
// get path | |
//string path = getSavePath (selectedObject); | |
string path = getSavePath (sd_num_charaname+"_"+AnimationNameStr+"_"+AnimationClipEventNameStr,"AnimationEvent",".asset"); | |
///////////////////////////////////////////////////////////////////////////// | |
/* | |
string SelectedObjectDir; | |
string selectedObjectStr = AssetDatabase.GetAssetPath (selectedObject); | |
Debug.Log ("--Crate-- selectedObjectStr=" + selectedObjectStr); | |
string[] modelDirArr; | |
modelDirArr=selectedObjectStr.Split("/"[0]); | |
string buildDir = modelDirArr[0]; | |
for(int i = 1; i < modelDirArr.Length-1; i++) | |
{ | |
buildDir = buildDir +"/"+ modelDirArr [i]; | |
} | |
SelectedObjectDir = buildDir; | |
Debug.Log ("--Crate-- SelectedObjectDir="+SelectedObjectDir); | |
*/ | |
//////////////////////////////////////////////////////////////////////////////// | |
// create instance | |
//ScriptableObject obj = ScriptableObject.CreateInstance (selectedObject.name); | |
//AnimationClipEventData | |
ScriptableObject obj = ScriptableObject.CreateInstance ("AnimationClipEventData"); | |
AssetDatabase.CreateAsset (obj, path); | |
// add label | |
ScriptableObject sobj = AssetDatabase.LoadAssetAtPath (path, typeof(ScriptableObject)) as ScriptableObject; | |
//sobj = AssetDatabase.LoadAssetAtPath (path, typeof(ScriptableObject)) as ScriptableObject; | |
AssetDatabase.SetLabels (sobj, labels); | |
EditorUtility.SetDirty (sobj); | |
ScriptableObjectRef = sobj; | |
//} | |
Debug.Log("--------Crate ----------end-----------"); | |
} | |
AnimationClip AnimationClipRef; | |
void CreateAnimationClip(string filename){ | |
string path = getSavePath (filename,"EditedAnimation",".anim"); | |
AnimationClip newClip = new AnimationClip(); | |
newClip.name = name; | |
AssetDatabase.CreateAsset(newClip, path); | |
// add label | |
AnimationClip obj = AssetDatabase.LoadAssetAtPath (path, typeof(AnimationClip)) as AnimationClip; | |
//sobj = AssetDatabase.LoadAssetAtPath (path, typeof(ScriptableObject)) as ScriptableObject; | |
AssetDatabase.SetLabels (obj, labels); | |
EditorUtility.SetDirty (obj); | |
AnimationClipRef = obj; | |
} | |
string getSavePath (string filename,string folderName,string filetype) | |
{ | |
Debug.Log("------- getSavePath ----- filename="+filename); | |
//string objectName = selectedObject.name; | |
//string objectName = selectedObject.name; | |
string objectName = filename; | |
//string objectName = selectedObject.name+"_AnimationClipEventData"; | |
if (filetype == ".asset") { | |
//objectName = selectedObject.name+"_"+AnimationNameStr+"_"+AnimationClipEventNameStr; | |
} | |
if (filetype == ".anim") { | |
//objectName =AnimationNameStr+"_event"; | |
} | |
//Debug.Log("------- getSavePath ----- objectName="+objectName); | |
//string dirPath = Path.GetDirectoryName (AssetDatabase.GetAssetPath (selectedObject)); | |
//////////// | |
string dirPath = CharaDir; | |
//string dirPath = CharaDir+"/AnimationClipEventData"; | |
////////// | |
Debug.Log ("------ getSavePath -----dirPath="+dirPath); | |
//string path = string.Format ("{0}/{1}.asset", dirPath, objectName); | |
//string path = dirPath+"/"+objectName+".asset"; | |
string path = dirPath+"/"+objectName+filetype; | |
//string AnimationEventDir = dirPath + "/AnimationEvent"; | |
string MakeDir = dirPath +"/"+ folderName; | |
if (AssetDatabase.IsValidFolder (MakeDir)) { | |
Debug.Log (MakeDir + " ありました!"); | |
} else { | |
Debug.Log (MakeDir+" ないのでつくりますです!"); | |
string guid = AssetDatabase.CreateFolder(dirPath, folderName); | |
string newFolderPath = AssetDatabase.GUIDToAssetPath(guid); | |
Debug.Log (newFolderPath+" = newFolderPath 作りました!"); | |
Debug.Log (MakeDir+" = MakeDir 作りました!"); | |
} | |
//path= MakeDir+"/"+objectName+".asset"; | |
path= MakeDir+"/"+objectName+filetype; | |
if (File.Exists (path)) { | |
for (int i = 1;; i++) { | |
//path = string.Format ("{0}/{1}({2}).asset", dirPath, objectName, i); | |
path=MakeDir+"/"+objectName+"_"+i+filetype; | |
if (!File.Exists (path)) | |
break; | |
} | |
} | |
Debug.Log("------ getSavePath --------- path="+path); | |
return path; | |
} | |
//////////////////////////////////////////////////////////////////////////////// | |
#region Static | |
/// <summary> | |
/// Open the tool window | |
/// </summary> | |
[MenuItem("Tools/Model/Animation Split And Make AnimeController")] | |
static public void OpenWindow () | |
{ | |
EditorWindow.GetWindow<ModelAnimationSplitterWithAnimeController> (true, "Model Animation Splitter With AnimeController", true); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment