zl程序教程

您现在的位置是:首页 >  其他

当前栏目

U3D 加在菜单栏加按键 和 动态生成资源束bundles

资源 生成 动态 按键 菜单栏
2023-09-27 14:25:49 时间

1, 加按键菜单

A, 在U3D的工程里任意新建一个脚本。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class test
{

	[MenuItem("Test/PrintPoem")]
	 static void WritePoem()
	{
		Debug.Log ("与君初相识,犹如故人归。天涯明月新,朝暮最相思。");
	}
}

注意:要引用名词空间 using UnityEditor; 这个类不用继承自MonoBehaviour; 函数必需是static.

B,保存后按F7/F8则



C, 单击Test下的printPoem


D,可以用这种方式来调试程序

先按F5在MonoBehaviour脚本中,再单击Test下的按钮printPoem,之后再按F10/F11调试。



2 ,Unity 关于AssetBundle(资源束)打包和加载

当是种情况时 [MenuItem("AssetBundle/Build (Collection)")],单击Build按钮前要先选中要导出的Prefabs,不然没反应。

Besides, we can refer to the unity3d Scripting API.

note:

A StartCoroutine(DownLoadAssetAndScene(BundleURL + "cub"));  

B Instantiate(bundle.LoadAsset("Cube.prefab"));

C, 打开或保存文件时,弹出窗口选择一个路径。

 string filepath = EditorUtility.SaveFilePanel("Save AO!", "", Selection.activeGameObject.name + "_AO.xml", "xml");
    /// 
    [MenuItem("ProcessPlan/Build (Single)")]
    static void Build_AssetBundle()
    {
        string pathStr = EditorUtility.OpenFolderPanel("Please select a Path", "", "");

        //BuildPipeline.BuildAssetBundles(Application.dataPath + "/Test_AssetBundle", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
        BuildPipeline.BuildAssetBundles(pathStr, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
        //刷新
        AssetDatabase.Refresh();
    }


C 打包脚本(脚本放在Editor文件夹下)


xml

<?xml version="1.0"?>
<!-- root name -->
<World Speed ="20.1245215500000010" Delay ="30000.0000000000000000">
    <!--
  Following is explaination of this scene XML defination.
  1.This file is defined using a TREE structure
  2.The root is world coordinate
  3.Each node and child node are defined Assembly or Part. Assembly is father node of Part.
  4.From root, can find every part in the scene
  5.Every part has attributes, include but not limited to: position, rotation, scale, model address
   
  
  How to use.
  1.The TREE includes an algorithm to traverse, insert, delete, modify each node
  2.Only analyzed and stored in Server, send ASCII code to Client for their sepcial requirement
  3.
  -->
    <Scene name="Scene 1" ID ="S01" FBX="file://D:/CPICode/U3DPrefabs/" Transform="0 0 0" Rotation="0 0 0" Scale="1 1 1">
        <Assembly name="Desk" ID="A01" FBX="file://D:/CPICode/U3DPrefabs/" Transform="0 0 0" Rotation="0 0 0" Scale="1 1 1">
			<Part name="desktop" ID="Cube.prefab" FBX= "file://D:/CPICode/U3DPrefabs/" Transform="2 2 2" Rotation="1 1 1" Scale="1 10 1"/>
		    <Part name="leg0" ID="Cylinder0.prefab" FBX="file://D:/CPICode/U3DPrefabs/" Transform="2 2 2" Rotation="1 1 1" Scale="1 10 1"/>
			<Part name="leg1" ID="Cylinder1.prefab" FBX="file://D:/CPICode/U3DPrefabs/" Transform="2 2 2" Rotation="1 1 1" Scale="1 10 1"/>
			<Part name="leg2" ID="Cylinder2.prefab" FBX="file://D:/CPICode/U3DPrefabs/" Transform="2 2 2" Rotation="1 1 1" Scale="1 10 1"/>
			<Part name="leg3" ID="Cylinder3.prefab" FBX="file://D:/CPICode/U3DPrefabs/" Transform="2 2 2" Rotation="1 1 1" Scale="1 10 1"/>
		</Assembly>
		<Part name="magicbox" ID="Box.prefab" FBX= "file://D:/CPICode/U3DPrefabs/" Transform="2 2 2" Rotation="1 1 1" Scale="1 10 1"/> 
    </Scene>
</World>



按之前的xml结构把对应的资源加载到unity

		void Update()
		{
			if (isLoadModel) {
				//print ("Hello poet and poem!");
				LoadFBX3DModel ();
				isLoadModel = false;
			}
		}
	

		private void LoadFBX3DModel()
		{

			Ass_1 = new GameObject ("Scene");
			GameObject World_0 = GameObject.Find ("World");
			Ass_1.transform.parent = World_0.transform;
			LoadAssPart (analyScene.scene, Ass_1);

		
//			//test success
//			//"file://D:/CPICode/U3DPrefabs/";
//			//StartCoroutine(DownLoadAssetAndScene(BundleURL + "leg"));
//		
//			//string fbxs = "file://D:/CPICode/U3DPrefabs/magicbox";
//
//			fbx = analyScene.scene.FBX_Address;
//			names = analyScene.scene.parts[0].Name;
//
//			char[] fbx_char_arry = fbx.ToCharArray();
//			char[] fbx_char = {'\0'};
//			fbx_string = fbx.TrimEnd (fbx_char);
//			//names_string = names.TrimEnd (fbx_char);//这个要不要都可以
//
//			paths = fbx_string + names;
//			StartCoroutine(DownLoadAssetAndScene(paths));

			//Michael Wang 20180403
		}



		private void LoadAssPart (AssemblyNode AssPart, GameObject AssOb)
		{
			for (int i = 0; i < AssPart.parts.Count; i++) {
				
				names = AssPart.parts [i].Name;
				fbx = AssPart.parts [i].FBX_Address;
				char[] fbx_char_arry = fbx.ToCharArray();
				char[] fbx_char = {'\0'};
				string fbx_string = fbx.TrimEnd (fbx_char);

				StartCoroutine(DownLoadAssetAndScene(fbx_string + names, AssPart.parts [i].ID.TrimEnd(fbx_char), AssOb));
			}
			//print (AssPart.parts.Count);
			//print (AssPart.assemblys.Count);
			for (int j = 0; j < AssPart.assemblys.Count; j++) {
				
				Ass_2 = new GameObject(AssPart.assemblys[0].Name);
				Ass_2.transform.parent = AssOb.transform;
				LoadAssPart ( AssPart.assemblys[j], Ass_2);
			}


		}


	


		IEnumerator DownLoadAssetAndScene(string path, string prefab, GameObject AssOb)
		{
			//电脑端加载使用
			WWW asset = new WWW(path);

			yield return asset;
			AssetBundle bundle = asset.assetBundle;
			//资源加载 
			//加载的是打包到资源包里面,每个资源的名字
			//Instantiate(bundle.LoadAsset("Cube.prefab"));
			//GameObject desk = (GameObject)Instantiate(bundle.LoadAsset("Cylinder.prefab"));
			//GameObject desk = (GameObject)Instantiate(bundle.LoadAsset("Box.prefab"));


			desk = (GameObject)Instantiate(bundle.LoadAsset(prefab));
			desk.transform.parent = AssOb.transform;

			//层级关系不对呀,哪个是哪的子节点和fu子点

			//资源加载完毕后记得Unload,不然再次加载资源的时候无法加载
			bundle.Unload(false);
		}
		//MichaelWang 20180329

对应的unity的树结构为



3, 

Unity学习笔记13——代码动态加载Prefab预设体

unity Editor自动生成材质及动态加载资源