当前位置:网站首页>Unity makes prefabricated bodies with one key and modifies prefabricated bodies with one key
Unity makes prefabricated bodies with one key and modifies prefabricated bodies with one key
2022-07-28 20:35:00 【Mark_ source】
Generate 、 Modify preform properties Used API
PrefabUtility.SaveAsPrefabAsset(GameObject instanceRoot, string assetPath);
PrefabUtility.SavePrefabAsset(GameObject asset);
AssetDatabase.LoadAssetAtPath(string assetPath, Type type);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
The specific use :
One 、 Making preforms
Get the source file path required by the prefab ( Source files are best placed in Resources Easy to load in the folder , It can be moved to a suitable position after the production )
The main method :
1)、 Get all folders under the path
/// <summary>
/// Get all folders under the path
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private string[] GetAllFolder(string path)
{
try
{
string[] dirs = Directory.GetDirectories(path, "*");
string[] folderName = new string[dirs.Length];
for (int i = 0; i < dirs.Length; i++)
{
string file = dirs[i].Split('\\')[1];
folderName[i] = file;
}
return folderName;
}
catch (System.Exception)
{
return null;
}
}
2)、 Get the file under the file
/// <summary>
/// Get... Under the path Obj
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private GameObject GetFileObj(string path)
{
GameObject go = Resources.Load<GameObject>(path);
if (go != null)
{
return go;
}
else
{
Debug.Log(path);
return null;
}
}
3)、 practice :
Import the source file for prefabrication , Look at the file structure
newly build Editor Folder And create a script MakePrefab
public class MakePrefab : EditorWindow
{
[MenuItem("Tools/MakePrefab")]
public static void CreatePrefabWindow()
{
EditorWindow window = EditorWindow.GetWindowWithRect(typeof(MakePrefab), new Rect(Screen.width / 3, Screen.height / 3, 800, 500), true, "MakePrefab");
window.Show();
}
}
Drawing panels
private static string toSavePrefabPath = "Assets/Entities";
private void OnGUI()
{
EditorGUILayout.LabelField(" Preform storage path ::", toSavePrefabPath, GUILayout.Width(110));
toSavePrefabPath = EditorGUILayout.TextArea(toSavePrefabPath, GUILayout.Width(250));
if (GUILayout.Button(" Conversion precast body ", GUILayout.Width(260)))
{
ToPrefab();
}
if (GUILayout.Button(" Modify the preform ", GUILayout.Width(260)))
{
ModifyPrefab(toSavePrefabPath);
}
}
Effect display
Making preforms
private void ToPrefab()
{
string path = "Assets/Resources";
string[] allFolder1 = GetAllFolder(path);
if (allFolder1 == null)
return;
// The number of cycles depends on , Directory structure of source files , Here is 4 Class structure
for (int i = 0; i < allFolder1.Length; i++)
{
string path2 = $"{path}/{allFolder1[i]}";
string[] allFolder2 = GetAllFolder(path2);
if (allFolder2 == null)
{
return;
}
for (int j = 0; j < allFolder2.Length; j++)
{
string path3 = $"{path2}/{allFolder2[j]}";
string[] allFolder3 = GetAllFolder(path3);
if (allFolder3 == null)
return;
for (int k = 0; k < allFolder3.Length; k++)
{
string path4 = $"{path3}/{allFolder3[k]}";
string[] allFolder4 = GetAllFolder(path4);
Debug.Log($" Number :{k} {allFolder3[k]} Under the document {allFolder4.Length} A folder !");
if (allFolder4 == null)
return;
if (!Directory.Exists(toSavePrefabPath))
{
Directory.CreateDirectory(toSavePrefabPath);
}
for (int l = 0; l < allFolder4.Length; l++)
{
string speedTree = $"{path4}/{allFolder4[l]}/{allFolder4[l]}";
string[] strs = Regex.Split(speedTree, path + "/", RegexOptions.IgnoreCase);
GameObject go = Instantiate(GetFileObj(strs[1]));
go.name = go.name.Replace("(Clone)", string.Empty);
// Add a script to the prefab , Modify properties
//go.transform.tag = "Model";
if (go.GetComponent<BoxCollider>() == null)
go.AddComponent<BoxCollider>();
if (go.GetComponent<Rigidbody>() == null)
go.AddComponent<Rigidbody>();
go.GetComponent<Rigidbody>().isKinematic = true;
string modeName = allFolder4[l].Split('_')[0];
PrefabUtility.SaveAsPrefabAsset(go, $"{toSavePrefabPath}/{modeName}.prefab");
DestroyImmediate(go);
}
AssetDatabase.Refresh();
}
}
}
}
Effect display
Two 、 Modify the preform :
When an attribute in the preform is set incorrectly , such as IsKinematic Attribute , This attribute needs to be modified uniformly
/// <summary>
/// Modify the preform
/// </summary>
/// <param name="path"></param>
private void ModifyPrefab(string path)
{
// Get all prefabricated body files under the file
DirectoryInfo info = new DirectoryInfo(path);
FileInfo[] fileInfos = info.GetFiles("*.prefab");
List<GameObject> prefabs = new List<GameObject>();
foreach (var item in fileInfos)
{
string paths = $"{path}/{item.Name}";
GameObject prefab = AssetDatabase.LoadAssetAtPath(paths, typeof(GameObject)) as GameObject;
prefabs.Add(prefab);
}
// Modify properties
for (int i = 0; i < prefabs.Count; i++)
{
if (prefabs[i].transform.childCount > 0 && prefabs[i].transform.GetChild(0) != null)
{
if (prefabs[i].GetComponent<Rigidbody>() != null)
{
prefabs[i].GetComponent<Rigidbody>().isKinematic = false;
PrefabUtility.SavePrefabAsset(prefabs[i]);
}
}
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
Effect display
In this way, all the prefabricated bodies have been made , And the properties in subsequent modifications !
边栏推荐
- UE4.25 Slate源码解读
- Linxu [permission, sticky bit]
- Configure Windows Server + install MySQL database on the server + Remote Access database
- C# 委托 delegate 的理解
- Torch. NN. Linear() function
- GRU神经网络
- 产品力大幅提升 新款福特探险者发布
- Linxu [basic instructions]
- Residual network RESNET source code analysis - pytoch version
- Use of DDR3 (axi4) in Xilinx vivado (5) board test
猜你喜欢
Use of DDR3 (axi4) in Xilinx vivado (2) read write design
Network shell
Raspberrypico serial communication
Why is customer support important to SaaS?
Scheduled backup of MySQL database under Windows system
太空射击第15课: 道具
数据挖掘(数据预处理篇)--笔记
Raspberrypico analytic PWM
Linxu 【基本指令】
Raspberry connects EC20 for PPP dialing
随机推荐
[C language] use function pointers to make a different calculator
Mysql报错:Specified key was too long; max key length is 767 bytes
CNN convolutional neural network structure
[C language] guessing numbers game
Shanghai Jiaotong University joined hands with Taobao to set up a media computing laboratory: promoting the development of key technologies such as video super score
LVS load balancing cluster
Soft raid
Use of DDR3 (axi4) in Xilinx vivado (4) incentive design
Linux Installation MySQL (pit filling version)
Wust-ctf2021-re school match WP
Nocturnal simulator settings agent cannot be saved
PXE_ KS unattended system
DOS common commands
太空射击第13课: 爆炸效果
同质化代币与 NFT 结合,如何使治理结构设计更灵活?
Scheduled backup of MySQL database under Windows system
Networkx common operations summary (for personal use)
[C language] comprehensively analyze the pointer and sort out the pointer knowledge points
Residual network RESNET source code analysis - pytoch version
The product power is greatly improved, and the new Ford Explorer is released