47 lines
2.1 KiB
C#
47 lines
2.1 KiB
C#
using UnityEngine;
|
|
using UnityEditor;
|
|
public class ConvertFromArtPrefabToGamePrefab : EditorWindow
|
|
{
|
|
[MenuItem("Tools/Creat IB Prefabs")]
|
|
public static void ConvertPrefabs()
|
|
{
|
|
Object[] artPrefabs = Selection.objects;
|
|
PhysicsMaterial2D pm = AssetDatabase.LoadAssetAtPath<PhysicsMaterial2D>("Assets/ABPackage/InfiniteBuildingPrefabs/PhysicMat_Block.physicsMaterial2D");
|
|
string dest = "Assets/ABPackage/InfiniteBuildingPrefabs/";
|
|
foreach (Object artGO in artPrefabs)
|
|
{
|
|
string assetPath = AssetDatabase.GetAssetPath(artGO);
|
|
GameObject artPrefab = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
|
|
GameObject artInstance = PrefabUtility.InstantiatePrefab(artPrefab) as GameObject;
|
|
Debug.Log(assetPath);
|
|
if (artInstance == null)
|
|
continue;
|
|
string artName = artPrefab.name;
|
|
BoxCollider artBc = artInstance.GetComponent<BoxCollider>();
|
|
Vector3 colliderSize = artBc.size;
|
|
Vector3 colliderCenter = artBc.center;
|
|
DestroyImmediate(artBc, true);
|
|
|
|
GameObject go = new GameObject();
|
|
go.name = artName;
|
|
go.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
|
|
go.transform.localScale = Vector3.one;
|
|
artInstance.transform.parent = go.transform;
|
|
artInstance.transform.position = Vector3.zero;
|
|
Rigidbody2D rb = go.AddComponent<Rigidbody2D>();
|
|
rb.drag = 4;
|
|
rb.angularDrag = 1;
|
|
BoxCollider2D bc = go.AddComponent<BoxCollider2D>();
|
|
bc.sharedMaterial = pm;
|
|
bc.isTrigger = false;
|
|
bc.offset = new Vector2(-colliderCenter.x, colliderCenter.z);
|
|
bc.size = new Vector2(colliderSize.x, colliderSize.z);
|
|
go.AddComponent<Block>();
|
|
PrefabUtility.SaveAsPrefabAsset(go, dest + go.name + ".prefab");
|
|
AssetDatabase.SaveAssets();
|
|
Debug.Log($"Converted colliders in prefab: {go.name}");
|
|
DestroyImmediate(go, true);
|
|
}
|
|
}
|
|
}
|