Skip to content

Instantly share code, notes, and snippets.

@Makizemi
Created July 26, 2018 07:22
Show Gist options
  • Save Makizemi/b6fba87611632ff3788b6196b06ab168 to your computer and use it in GitHub Desktop.
Save Makizemi/b6fba87611632ff3788b6196b06ab168 to your computer and use it in GitHub Desktop.
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
using System.IO;
public class PrefabCreator : EditorWindow
{
const string MenuPath = "Assets/Create/CreatePrefab #\\";
readonly static string[] ColliderName =
{
"FUC_Box",
"FUC_Sphere",
};
delegate void AddCollider(GameObject o, int index);
static readonly AddCollider[] AddColliderMethod =
{
new AddCollider(AddBoxCollider),
new AddCollider(AddSphereCollider),
};
/// <summary>
/// 選択されているアセットのprefabを全て作成する
/// </summary>
[MenuItem(MenuPath)]
static void CreatePrefabs()
{
GameObject[] objectArray = Selection.gameObjects;
List<string> pathList = new List<string>();
//モデルと同じディレクトリにプレハブを作成するため、パスを取得する
AddFolderPath(pathList);
for (int i = 0; i < objectArray.Length; i++)
{
string localPath = pathList[i] + ".prefab";
if (AssetDatabase.LoadAssetAtPath(localPath, typeof(GameObject)))
{
if (EditorUtility.DisplayDialog("よろしいですか?",
"もうすでにこのプレハブありますよ?上書きしますか?",
"はい",
"いいえ"))
{
CreateNewPrefab(objectArray[i], localPath);
}
continue;
}
Debug.Log(objectArray[i].name + "をColliderコンポーネントを追加してプレハブ化しました。");
CreateNewPrefab(objectArray[i], localPath);
}
}
static void AddFolderPath(List<string> pathList)
{
foreach (var files in Selection.assetGUIDs)
{
var path = AssetDatabase.GUIDToAssetPath(files);
var extension = Path.GetExtension(path);
path = path.Replace(extension, string.Empty);
pathList.Add(path);
}
}
static void CreateNewPrefab(GameObject obj, string localPath)
{
GameObject prefab = PrefabUtility.CreatePrefab(localPath, obj);
GameObject o = PrefabUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ConnectToPrefab);
for (int i = 0; i < ColliderName.Length; i++)
{
if (o.transform.Find(ColliderName[i]))
{
AddColliderMethod[i](o, i);
break;
}
}
}
static void AddBoxCollider(GameObject o, int index)
{
o.AddComponent<BoxCollider>();
Transform colliderTransform = o.transform.Find(ColliderName[index]);
(o.GetComponent<Collider>() as BoxCollider).center = colliderTransform.position;
(o.GetComponent<Collider>() as BoxCollider).size = colliderTransform.localScale;
//当たり判定作成用のメッシュは不要になったので削除する
DestroyImmediate(o.transform.Find(ColliderName[index]).gameObject, true);
}
static void AddSphereCollider(GameObject o, int index)
{
o.AddComponent<SphereCollider>();
Transform colliderTransform = o.transform.Find(ColliderName[index]);
(o.GetComponent<Collider>() as SphereCollider).center = colliderTransform.position;
(o.GetComponent<Collider>() as SphereCollider).radius = colliderTransform.localScale.x;
//当たり判定作成用のメッシュは不要になったので削除する
DestroyImmediate(o.transform.Find(ColliderName[index]).gameObject, true);
}
[MenuItem(MenuPath, true)]
static bool ValidateCreatePrefab()
{
return Selection.activeGameObject != null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment