Last active
August 29, 2015 14:25
-
-
Save whaison/96eee357b27231a03218 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; | |
//public class SdDataToEffectPrefabMaker : MonoBehaviour { | |
public class Ef_pngToSprite2dAnimation : EditorWindow { | |
//string FolderName="sd_0000_name"; | |
//Object SelectionObject; | |
TextAsset text; | |
Texture inputTexture; | |
Texture outTexture; | |
TextureImporter textureImporter; | |
string[] DirArr; | |
string[] SDCharaNameArr; | |
string[] Fix_DirArr; | |
string Fix_ef_png_filename; | |
string[] Fix_CharaNameArr; | |
string Fix_ef_charaName; | |
string CharaFolderName; | |
string CreatedFolder; | |
string ef_Dir; | |
string DirStr; | |
string ef_Id ; | |
string ef_charaId; | |
string ef_charaNam; | |
string ef_charaName; | |
string ef_png_filename; | |
AnimatorStateMachine BaseLayerStateMachine; | |
UnityEditor.Animations.AnimatorController AnimatorControllerRef; | |
AnimationClip AnimationClipRef; | |
List<GameObject> Sprite2dGameObjectRefList = new List<GameObject>(); | |
List<GameObject> ef_prefabRefList = new List<GameObject>(); | |
Material myMaterial; | |
private int selGridInt = 0; | |
private string[] selCaptions = new string[] { | |
"attack", | |
"skill", | |
}; | |
private int shaderInt = 0; | |
private string[] selectShadersArr = new string[] { | |
"Sprite/Default", | |
"Sprites/DefaultAdditive", | |
}; | |
string AttackOrSkill; | |
void OnGUI () | |
{ | |
GUILayout.Label ("png To Sprite2d Animation", 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 (10f); | |
selGridInt = GUILayout.SelectionGrid(selGridInt, selCaptions, 2, "Toggle"); | |
GUILayout.Space (10f); | |
shaderInt = GUILayout.SelectionGrid(shaderInt, selectShadersArr, 2, "Toggle"); | |
GUILayout.Space (10f); | |
inputTexture = EditorGUILayout.ObjectField ("png", inputTexture, typeof(Texture), false) as Texture; | |
if (inputTexture == null) { | |
GUILayout.Label ("Set png or other image object."); | |
} | |
//go = Selection.activeGameObject; | |
//FolderName = go.name; | |
//GUILayout.Label ("SD Data Folder To Effect Folder and Prefab", EditorStyles.boldLabel); | |
GUILayout.Space (20f); | |
if (GUILayout.Button ("png To Sprite2d Animation", GUILayout.Width (300f))) { | |
pngToSpriteAnimation (); | |
} | |
} | |
void pngToSpriteAnimation(){ | |
Sprite2dGameObjectRefList = new List<GameObject>(); | |
//foreach (Object selectedObject in Selection.objects) { | |
// Debug.Log ("Crate selectedObject=" + selectedObject); | |
// SelectionObject = selectedObject; | |
// FolderName = SelectionObject.name; | |
//} | |
AttackOrSkill = selCaptions [selGridInt]; | |
Debug.Log ("AttackOrSkill=" + AttackOrSkill); | |
//string DirStr = AssetDatabase.GetAssetPath (texture); | |
DirStr = AssetDatabase.GetAssetPath (inputTexture); | |
Debug.Log ("DirStr=" + DirStr); | |
//Close (); | |
Debug.Log ("------MakeFolderStart------"); | |
string[] DirArr; | |
DirArr = DirStr.Split ("/" [0]); | |
string buildDir = DirArr [0]; | |
for (int i = 1; i < DirArr.Length - 1; i++) { | |
buildDir = buildDir + "/" + DirArr [i]; | |
} | |
ef_Dir = buildDir; | |
Debug.Log ("ef_Dir=" + ef_Dir); | |
ef_png_filename = DirArr [DirArr.Length - 1]; | |
Debug.Log ("ef_png_filename=" + ef_png_filename);//ef_60110_gina_png.png | |
string[] ef_png_filename_arr; | |
ef_png_filename_arr = ef_png_filename.Split ("_" [0]); | |
ef_Id = ef_png_filename_arr [1]; | |
Debug.Log ("ef_Id=" + ef_Id); | |
ef_charaId = ef_png_filename_arr [0] + "_" + ef_png_filename_arr [1]; | |
Debug.Log ("ef_charaId=" + ef_charaId); | |
ef_charaName = ef_png_filename_arr [0] + "_" + ef_png_filename_arr [1] + "_" + ef_png_filename_arr [2]+"_"+AttackOrSkill; | |
Debug.Log ("ef_charaName=" + ef_charaName); | |
if (shaderInt == 1) { | |
// Create a simple material asset | |
myMaterial = new Material (Shader.Find ("Sprites/DefaultAdditive")); | |
string MaterialPath = ef_Dir + "/" + ef_png_filename_arr [0] + "_" + ef_png_filename_arr [1] + "_" + ef_png_filename_arr [2] + "_sprite2d_mat.mat"; | |
Debug.Log ("--------------MaterialPath=" + MaterialPath + "---------------------"); | |
if (File.Exists (MaterialPath)) { | |
myMaterial= AssetDatabase.LoadAssetAtPath(MaterialPath, typeof(Material)) as Material; | |
} else { | |
AssetDatabase.CreateAsset (myMaterial, MaterialPath); | |
} | |
} | |
//------------texture2d----------------------------------------------- | |
//texture2d.mo | |
//TextureImporterSettings textureImporterSettings; | |
//TextureImporter.SetTextureSettings (textureImporterSettings); | |
//TextureImporter.textureType = TextureImporterType.Sprite; | |
string texturePath = DirStr; | |
//TextureImporter importer | |
textureImporter = TextureImporter.GetAtPath (texturePath) as TextureImporter; | |
textureImporter.textureType = TextureImporterType.Sprite; | |
textureImporter.spriteImportMode = SpriteImportMode.Multiple; | |
textureImporter.filterMode = FilterMode.Point; | |
EditorUtility.SetDirty (textureImporter); | |
AssetDatabase.ImportAsset (texturePath, ImportAssetOptions.ForceUpdate); | |
//Texture texture | |
Texture outTexture = AssetDatabase.LoadAssetAtPath (texturePath, typeof(Texture)) as Texture; | |
//importer.spritePixelsPerUnit = Mathf.Max (texture.width / horizontalCount, texture.height / verticalCount); | |
textureImporter.spritePixelsPerUnit = 1.0f; | |
textureImporter.spritesheet = CreateSpriteMetaDataArray (outTexture); | |
EditorUtility.SetDirty (textureImporter); | |
AssetDatabase.ImportAsset (texturePath, ImportAssetOptions.ForceUpdate); | |
//------------texture2d----------------------------------------------- | |
sprite2dInstanciate (); | |
} | |
SpriteMetaData[] CreateSpriteMetaDataArray (Texture texture) | |
{ | |
List<SpriteMetaData> SpriteMetaDataList = new List<SpriteMetaData>(); | |
for (int i = 0; i < 6+1; i++) { | |
string namestr = texture.name + "_" + i; | |
Rect rectBox = new Rect (0, 0, 0, 0); | |
switch(i){ | |
case 0: | |
rectBox = new Rect (128,128, 128, 128);break; | |
case 1: | |
rectBox = new Rect (0,128, 128, 128);break; | |
case 2: | |
rectBox = new Rect (128,0, 128, 128);break; | |
case 3: | |
rectBox = new Rect (64,64, 64, 64);break; | |
case 4: | |
rectBox = new Rect (0,64, 64, 64);break; | |
case 5: | |
rectBox = new Rect (64,0, 64, 64);break; | |
case 6: | |
rectBox = new Rect (0, 0, 64, 64);break; | |
default: | |
break; | |
} | |
//rect = new Rect (spriteWidth * x, texture.height - spriteHeight * (y + 1), spriteWidth, spriteHeight); | |
SpriteMetaData spriteMetaData = new SpriteMetaData (); | |
spriteMetaData.name=namestr; | |
spriteMetaData.rect = rectBox; | |
SpriteMetaDataList.Add (spriteMetaData); | |
} | |
return SpriteMetaDataList.ToArray (); | |
} | |
void sprite2dInstanciate(){ | |
Debug.Log ("--------sprite2dInstanciate-----------"); | |
//string[] TextureDependencies = AssetDatabase.GetDependencies( DirStr); | |
// Create | |
//GameObject gameObject = new GameObject(); | |
string ef_instanceName = ef_charaName; | |
string ef_gameObjectName=ef_instanceName+".prefab"; | |
//AssetDatabase.CreateAsset(gameObject, CreateEffectFolderName+"/"+EffectFolderName+"/"+gameObjectName); | |
//string ef_gameObjectPath = ef_Dir+ "/" + ef_gameObjectName; | |
//################################################################################################ | |
string ef_gameObjectPath = getSavePath (ef_instanceName, "", ".prefab"); | |
Debug.Log ("ef_gameObjectPath getSavePath = "+ef_gameObjectPath); | |
Fix_DirArr = ef_gameObjectPath.Split ("/" [0]); | |
Fix_ef_png_filename = Fix_DirArr [Fix_DirArr.Length - 1]; | |
Fix_CharaNameArr= Fix_ef_png_filename.Split ("." [0]); | |
Fix_ef_charaName = Fix_CharaNameArr [0]; | |
ef_instanceName = Fix_ef_charaName; | |
ef_gameObjectName=ef_instanceName+".prefab"; | |
/* | |
string[] Fix_CharaNameUnderArr=Fix_ef_charaName.Split ("_" [0]); | |
string buildFixedCharaNameStr=""; | |
for (int l = 0; l < Fix_CharaNameUnderArr.Length-1; l++) { | |
buildFixedCharaNameStr = buildFixedCharaNameStr + Fix_CharaNameUnderArr [l]; | |
if (l < Fix_CharaNameUnderArr.Length - 2) { | |
buildFixedCharaNameStr = buildFixedCharaNameStr +"_"; | |
} | |
} | |
string Fixed_ef_charaName =buildFixedCharaNameStr; | |
Debug.Log ("Fixed_ef_charaName= "+Fixed_ef_charaName); | |
*/ | |
//################################################################################################ | |
GameObject ef_Instance = EditorUtility.CreateGameObjectWithHideFlags (ef_gameObjectName, HideFlags.HideInHierarchy); | |
//PrefabUtility.CreateEmptyPrefab (fe_gameObjectPath, ef_Instance); | |
GameObject ef_prefab=PrefabUtility.CreatePrefab (ef_gameObjectPath, ef_Instance); | |
//AnimatorContraller | |
string ACpath; | |
//################################################################################################ | |
ACpath = getSavePath (ef_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); | |
UnityEditor.Animations.AnimatorState state001 = BaseLayerStateMachine.AddState(ef_charaName,new Vector3(0,150,0)); | |
CreateAnimationClip (ef_charaName); | |
state001.motion = AnimationClipRef; | |
//BaseLayerStateMachine.AddState ("Default State", new Vector3 (-250, -100,0)); | |
//AddComponent Animator | |
Animator ef_Animator =ef_prefab.AddComponent<Animator> (); | |
ef_Animator.runtimeAnimatorController = AnimatorControllerRef; | |
//AddComponent | |
// BasicEffectController ef_basicEffectController =ef_prefab.AddComponent<BasicEffectController> (); | |
//ef_basicEffectController.useEndOfTime = true; | |
// ef_basicEffectController.endOfTime = 1.5f; | |
//プレファブをインスタンシエイト | |
GameObject ef_InstanceAttachedPrefab=PrefabUtility.InstantiateAttachedAsset (ef_prefab)as GameObject; | |
ef_InstanceAttachedPrefab.name = ef_instanceName; | |
//プレハブをApplyする | |
PrefabUtility.ReplacePrefab (ef_InstanceAttachedPrefab, ef_prefab, ReplacePrefabOptions.ConnectToPrefab); | |
//Assetをリロードする。 | |
AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); | |
/**/ | |
//GameObject ef_Instance = EditorUtility.CreateGameObjectWithHideFlags (ef_instanceName, HideFlags.None); | |
//Debug.Log ("--------TextureDependencies-----="+TextureDependencies.Length+""); | |
Debug.Log ("--------textureImporter.spritesheet.Length-----------"+textureImporter.spritesheet.Length); | |
for (int i = 0; i < textureImporter.spritesheet.Length; i++) | |
{ | |
string sprit2dName=textureImporter.spritesheet [i].name; | |
Debug.Log ("--------sprit2dName-----------"+sprit2dName); | |
GameObject sprite_Instance = EditorUtility.CreateGameObjectWithHideFlags (sprit2dName, HideFlags.HideInHierarchy); | |
sprite_Instance.name = sprit2dName; | |
string sprit2dprefabPath=ef_Dir+ "/" + sprit2dName+"_prefab.prefab"; | |
//PrefabUtility.CreateEmptyPrefab (fe_gameObjectPath, ef_Instance); | |
//プレファブ作る | |
GameObject sprite_prefab=PrefabUtility.CreatePrefab (sprit2dprefabPath, sprite_Instance); | |
SpriteRenderer sprite_SpriteRenderer = sprite_prefab.AddComponent<SpriteRenderer> (); | |
//string sprite2dAssetPass=ef_Dir+ "/" + sprit2dName; | |
string sprite2dAssetPass=ef_Dir+"/"+ef_png_filename; | |
Debug.Log ("sprite2dAssetPass= "+sprite2dAssetPass+" の中身は"); | |
Sprite mysprite= seachAseet (sprite2dAssetPass, sprit2dName) as Sprite; | |
Debug.Log ("mysprite="+mysprite); | |
sprite_SpriteRenderer.sprite=mysprite; | |
if (shaderInt == 1) { | |
//マテリアルを設定 | |
sprite_SpriteRenderer.material = myMaterial; | |
} | |
//プレファブをインスタンシエイト | |
GameObject spriteInstantiateAttachePrefab=PrefabUtility.InstantiateAttachedAsset (sprite_prefab)as GameObject; | |
spriteInstantiateAttachePrefab.name = sprit2dName; | |
//親を設定 | |
spriteInstantiateAttachePrefab.transform.parent = ef_InstanceAttachedPrefab.transform; | |
//リストに参照を保存。 | |
Sprite2dGameObjectRefList.Add (spriteInstantiateAttachePrefab); | |
//いらないプレファブ消す | |
AssetDatabase.DeleteAsset (sprit2dprefabPath); | |
//ef_Dir | |
} | |
//プレハブをApplyする | |
PrefabUtility.ReplacePrefab (ef_InstanceAttachedPrefab, ef_prefab, ReplacePrefabOptions.ConnectToPrefab); | |
//Assetをリロードする。 | |
AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); | |
} | |
public UnityEngine.Object seachAseet (string filepath,string seachName){ | |
UnityEngine.Object MyObject=new UnityEngine.Object();; | |
//Sprite mysprite = UnityEditor.AssetDatabase.LoadAssetsAtPath(sprite2dAssetPass, typeof(Sprite)) as Sprite; | |
UnityEngine.Object[] FolderAssetArr = UnityEditor.AssetDatabase.LoadAllAssetsAtPath (filepath); | |
//UnityEngine.Object[] FolderAssetArr = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath (sprite2dAssetPass); | |
Debug.Log ("FolderAssetArr.Length="+FolderAssetArr.Length+"です。"); | |
for (int j = 0; j< FolderAssetArr.Length;j++) { | |
string AssetName=FolderAssetArr [j].name; | |
Debug.Log ("AssetName="+AssetName); | |
if (seachName == AssetName) { | |
Debug.Log ("HIT!!!!! seachName="+seachName+" AssetName="+AssetName+"-------OK"); | |
MyObject = FolderAssetArr [j]; | |
} | |
} | |
/* | |
if (MyObject == null) { | |
MyObject=new UnityEngine.Object(); | |
Debug.Log("エラー発生です。"); | |
} | |
*/ | |
return MyObject; | |
} | |
string[] labels = {"Data", "ScriptableObject"}; | |
void CreateAnimationClip(string filename){ | |
string path = getSavePath (filename,"",".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) | |
{ | |
string undot_filetype=filetype.Substring (1); | |
//string under_filetype="_"+undot_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 = ef_Dir; | |
//string dirPath = CharaDir+"/AnimationClipEventData"; | |
////////// | |
//string path = string.Format ("{0}/{1}.asset", dirPath, objectName); | |
//string path = dirPath+"/"+objectName+".asset"; | |
string path = dirPath+"/"+objectName+"_1"+"_"+undot_filetype+filetype; | |
Debug.Log ("------ getSavePath -----path="+path); | |
//string AnimationEventDir = dirPath + "/AnimationEvent"; | |
string MakeDir; | |
if (folderName == "") { | |
MakeDir = dirPath; | |
} else { | |
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+"_1"+"_"+undot_filetype+filetype; | |
} | |
Debug.Log ("------ getSavePath -----path="+path); | |
if (File.Exists (path)) { | |
Debug.Log ("------ getSavePath -----path=" + path + "あったので"); | |
for (int i = 1;; i++) { | |
//path = string.Format ("{0}/{1}({2}).asset", dirPath, objectName, i); | |
path = MakeDir + "/" + objectName + "_" + i + "_" + undot_filetype + filetype; | |
Debug.Log ("------ getSavePath -----path=" + path + "足しました。"); | |
if (!File.Exists (path)) { | |
break; | |
} | |
} | |
} else { | |
Debug.Log ("------ getSavePath -----path=" + path + "なかったのでそのままです。"); | |
} | |
Debug.Log("------ getSavePath --------- path="+path); | |
return path; | |
} | |
public void templateCode() | |
{ | |
} | |
/// <summary> | |
/// 指定したパスにディレクトリが存在しない場合 | |
/// すべてのディレクトリとサブディレクトリを作成します | |
/// </summary> | |
public System.IO.DirectoryInfo SafeCreateDirectory(string path,string foldername) | |
{ | |
string dirpathName = path +"/"+ foldername; | |
if (Directory.Exists(dirpathName)) | |
{ | |
Debug.Log ("そのフォルダはすでに存在してます。"); | |
return null; | |
} | |
return Directory.CreateDirectory(dirpathName); | |
} | |
//////////////////////////////////////////////////////////////////////////////// | |
#region Static | |
/// <summary> | |
/// Open the tool window | |
/// </summary> | |
[MenuItem("Tools/Effect/png To Sprite2d Animation")] | |
static public void OpenWindow3 () | |
{ | |
EditorWindow.GetWindow<Ef_pngToSprite2dAnimation> (true, "png To Sprite2d Animation", true); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment