unity3d场景 不小心删除了场景中的对象 怎么恢复

查看: 609|回复: 3
Unity3D ScrollView 使用对象池动态添加与删除元素
主题帖子积分
论坛元老, 积分 435735, 距离下一级还需 9564264 积分
论坛元老, 积分 435735, 距离下一级还需 9564264 积分
ScrollView 在开发的过程中使用非常普遍,但是我们在动态处理元素的时候,如果频繁的创建与销毁元素,那么 ScrollView 的性能也会降低,因为 UIGrid 自身并没有对象池,所以需要我们额外去处理。&&?4 F! V% J+ S, ^4 `$ s1 R5 H
& &先来看看最终效果,默认界面如图:
56_7853_new.jpg (115.47 KB, 下载次数: 0)
13:47 上传
& &当点击装备的时候,程序会筛选出装备分类的物品并显示,那么多出来的子元素会从 UIGrid 对象中删除,并被缓存到对象池中,等待需要的时候再次赋给 UIGrid,这样元素的个数只与对象的最大个数相同,如图:/ \&&b& o, G1 `+ m- p. U; V1 n( A
12_4103_new.jpg (104.88 KB, 下载次数: 0)
13:47 上传
+ ~$ U. z: g1 U+ w$ N, u$ b
& &先来搭建测试场景,我们需要额外添加一个 CacheItem 对象,并且不激活这个对象,因为不希望缓存起来的对象能被显示,如图:, ~! t) Y& Z- J% {' |
24_1291_new.jpg (136.68 KB, 下载次数: 0)
13:47 上传
& &限于篇幅,因为与核心模块无关的代码比较多,所以这儿只展示核心模块代码的创建与使用,剩下的代码可以直接下载源代码进行查看,新建立一个新的 C# 类,取名:StoreItemList.cs 代码如下:using UnityE2 u* e+ O* c& {
using System.Collections.G7 U7 x5 D2 |& W
! J) l& F- e( W. |3 w( x7 P3 M
public class StoreItemList : MonoBehaviour
{- W& z1 M; u0 |2 M&&~
/// &summary&6 _+ l6 Q3 p: w% m" [&&_* y
/// 物品格子0 v1 E5 V6 T' K( ?8 l' \
/// &/summary&2 E3 Z4 ^4 y8 u' l. W& D
public StoreItem storeI1 K# S7 X: ^. u* @$ P&&l
/// &summary&
/// 缓存元件
/// &/summary&7 o4 k& p" S2 Q! ~
public GameObject cacheI6 Z+ X+ n& ^2 A
/// &summary&
/// uiGrid 对象
/// &/summary&
private UIGrid uiG- ^+ D% a1 q
/ T: ~& V" d, `, l+ u
/// &summary&
/// 物品格子列表- k! p4 ]: Q, z$ ]3 z2 {
/// &/summary&: p( U& {5 x/ S/ f* T
private IList&StoreItem& itemL/ K8 I+ S$ `, x+ S4 e% }& Z
7 H1 o' c/ _9 I% F
/// &summary&
/// 缓存格子列表,可以直接使用 ItemList 列表,但这样会稍微麻烦一些
/// &/summary&
private IList&StoreItem& cacheItemL&&}" Q2 z/ M8 Q/ C2 P/ ~0 o
+ _4 U0 [! u&&O3 |) w" ~
void Awake(); n2 A' }' W0 }: ^; B9 r' x, d
{! f7 K) e4 k1 L0 h
&&this.uiGrid = this.GetComponentInParent&UIGrid& ();
&&this.itemList = new List&StoreItem& ();
&&this.cacheItemList = new List&StoreItem& ();
- }2 i- E&&e6 Q5 s$ C% }
/// &summary&
/// 更新物品栏! T8 M3 I) i$ ?! U1 p9 T1 I3 @
/// &/summary&
public void ChangeData(IList&ItemModel& modelList)/ o- O7 E&&F. C% S! ?+ `
&&GameObject newObject =
&&StoreItem newItem =- f* X' c, H, E$ _* w) f) y
6 s- l2 I; `3 G0 ?
&&int dataLength = modelList.C- M1 {+ E/ V5 P* v5 \
&&int itemLength = this.itemList.C$ q& V: `6 m7 f6 j+ k8 Z&&V
&&int index = 0;
. |3 J4 n2 F' \5 n/ D( @
&&for(index = 0; index & dataL index ++)" @& a. d. ]5 O
&&{/ C: f4 m7 c+ A: ?0 a0 T
& &if(index & itemLength)
& &{+ d&&n. n# k# N- G
& & newObject = this.itemList[index].gameO+ T" }: \&&Q5 a1 X, t
& & newItem = this.itemList[index];
& &}7 F! y&&h6 b' k
& &else4 T$ @) }* e9 v2 ]# d
& & // 如果有缓存格子数据2 R) D; d, g6 ^! ?8 J% H
& & if(this.cacheItemList.Count & 0)6 P* l! H&&Y1 Q- u" W% l
& &&&newObject = this.cacheItemList[0].gameO! ^- j& E+ }( J, G" R/ I8 k
& &&&newItem = this.cacheItemList[0];
& &&&newObject.transform.parent = this.9 Z! s- Y' T- ]2 T8 J0 {) H
& &&&this.cacheItemList.RemoveAt(0);
& &&&this.itemList.Add(newItem);
& & }else{
& &&&newObject = (GameObject)Instantiate(this.storeItem.gameObject);$ x+ I" ?9 f/ f, i
& &&&if(newObject != null)
& && &// 设置父对象&&W4 C5 j, Z6 B$ p) W2 H
& && &newObject.transform.parent = this.. Z3 K2 P& O* _5 G& \7 {9 s, p4 m
& && &newItem = newObject.GetComponent&StoreItem&();
& && &// 物品脚本" e4 T. x9 X2 Y/ J9 f5 b&&l" c$ D
& && &if(newItem != null)
& && &{! y( C: I. w" g
& && & newItem.Init(OnItemCallback);
& && & this.itemList.Add(newItem);3 T' Y- J$ q' z9 `% K
& & }% j$ X. y& l8 N8 b, y
& &}8 o7 s0 T$ Q! s5 H# ?& X+ _
& &if(newObject != null)
& & newObject.transform.localPosition = new Vector3(0f, 0f, 0f);
& & newObject.transform.localScale = new Vector3(1f, 1f, 1f);( y" B' V0 Y- g: l* B
& &// 更新物品格子数据2 J1 b' g. N1 _' H
& &if(newItem != null) newItem.ChangeData(modelList[index]);
&&// 如果格子超出
&&while(index & this.itemList.Count)
&&{4 x# f# F( b3 X& o
& &int endIndex = this.itemList.Count - 1;
& &// 从 UIGrid 中删除
& &this.itemList[endIndex].transform.parent = this.cacheItem.
& &// 添加到缓存列表中
& &this.cacheItemList.Add(this.itemList[endIndex]);& ^" N! \, ^* ^( v) y+ p# `% \
& &// 从列表中删除
& &this.itemList.RemoveAt(endIndex);
&&}&&i) Q4 z&&m* ?6 k
&&// 刷新 UI
&&this.uiGrid.Reposition ();! r% d% H, q7 {2 L9 x4 R6 K7 f
private void OnItemCallback(ItemModel itemModel)
( ~% A0 P& G8 G
}8 A5 E5 H& k0 V; p( G( T
& &然后把 StoreItemList.cs 挂载到 GridList 对象上(包含UIGrid组件),如图:0 Y. Y% \. D& M2 P
00_9260_new.jpg (132.54 KB, 下载次数: 0)
13:47 上传
& &最后运行游戏,查看效果。
游戏咖啡屋-最好的游戏编程源码技术网站!1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关 交流QQ群:
2、本站所有主题由该帖子作者发表,该帖子作者与享有帖子相关版权
4、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
哈...今天心情不错,来游戏咖啡区签到了...
主题帖子积分
论坛元老, 积分 13227, 距离下一级还需 9986772 积分
论坛元老, 积分 13227, 距离下一级还需 9986772 积分
好好,以前看过的文章都是收集过来啦& && && && &
哎...今天够累的,签到来了游戏源码下载...
主题帖子积分
论坛元老, 积分 13227, 距离下一级还需 9986772 积分
论坛元老, 积分 13227, 距离下一级还需 9986772 积分
因为与核心模块无关的代码比较多,所以这儿只展示核心模块代码的创建与使用,剩下的代码可以直接下载源代码进行查看& && && & 源码下载地址呢
哎...今天够累的,签到来了游戏源码下载...
主题帖子积分
注册会员, 积分 52, 距离下一级还需 148 积分
注册会员, 积分 52, 距离下一级还需 148 积分
2 v' M# x0 s- G
好好,源码下载地址呢
注册账号后积极发帖的会员
积极宣传本站,为本站带来更多的用户访问量
经常在论坛发帖,且发帖量较大
曾经为论坛做出突出贡献目前已离职的版主
Powered by给我们留言
地址:福建省晋江市青阳街道洪山路国际工业设计园纳金网
电话: 400-067-3919
(周一到周五, 周六周日休息)
设计师入口
查看: 1562|回复: 1
最后登录注册时间阅读权限90积分32484
资深设计师, 积分 32484, 距离下一级还需 17516 积分
纳金币16357 精华132
主要功能:
& & & & 1.导出场景的配置文件
& & & & 2.导出当前场景中资源的AssetBundle
& & & & 3.客户端从服务器获取配置文件
& & & & 4.解析配置文件,并根据配置文件下载AssetBundle
& & & & 5.实例化并还原场景
1.场景设置:将需要导出的场景资源设置为预设
2.将场景配置导出为XML文件using UnityE
using UnityE
using System.C
using System.Collections.G
using System.X
using System.IO;
using System.T
public class ExportSceneToXml : Editor
{
& & & & [MenuItem(&Assets/Export Scene To XML From Selection&)]
& & & & static void ExportXML()
& & & & {
& & & & & & & & string path = EditorUtility.SaveFilePanel (&Save Resource&, &&, &New Resource&, &xml&);
& & & & & & & & if (path.Length != 0)
& & & & & & & & {
& & & & & & & & & & & & Object[] selectedAssetList = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);
& & & & & & & & & & & &
& & & & & & & & & & & & //遍历所有的游戏对象
& & & & & & & & & & & & foreach (Object selectObject in selectedAssetList)
& & & & & & & & & & & & {
& & & & & & & & & & & & & & & & // 场景名称
& & & & & & & & & & & & & & & & string sceneName = selectObject.
& & & & & & & & & & & & & & & & // 场景路径
& & & & & & & & & & & & & & & & string scenePath = AssetDatabase.GetAssetPath(selectObject);
& & & & & & & & & & & & & & & & // 场景文件
& & & & & & & & & & & & & & & & //string xmlPath = //Application.dataPath + &/AssetBundles/Prefab/Scenes/& + sceneName + &.xml&;
& & & & & & & & & & & & & & & & // 如果存在场景文件,删除
& & & & & & & & & & & & & & & & if(File.Exists(path)) File.Delete(path);
& & & & & & & & & & & & & & & & // 打开这个关卡
& & & & & & & & & & & & & & & & EditorApplication.OpenScene(scenePath);
& & & & & & & & & & & & & & & & XmlDocument xmlDocument = new XmlDocument();
& & & & & & & & & & & & & & & & // 创建XML属性
& & & & & & & & & & & & & & & & XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration(&1.0&, &utf-8&, null);
& & & & & & & & & & & & & & & & xmlDocument.AppendChild(xmlDeclaration);
& & & & & & & & & & & & & & & & // 创建XML根标志
& & & & & & & & & & & & & & & & XmlElement rootXmlElement = xmlDocument.CreateElement(&root&);
& & & & & & & & & & & & & & & & // 创建场景标志
& & & & & & & & & & & & & & & & XmlElement sceneXmlElement = xmlDocument.CreateElement(&scene&);
& & & & & & & & & & & & & & & & sceneXmlElement.SetAttribute(&sceneName&, sceneName);
& & & & & & & & & & & & & & & &
& & & & & & & & & & & & & & & & foreach (GameObject sceneObject in Object.FindObjectsOfType(typeof(GameObject)))
& & & & & & & & & & & & & & & & {
& & & & & & & & & & & & & & & & & & & & // 如果对象是激活状态
& & & & & & & & & & & & & & & & & & & & if (sceneObject.transform.parent == null && sceneObject.activeSelf)
& & & & & & & & & & & & & & & & & & & & {
& & & & & & & & & & & & & & & & & & & & & & & & // 判断是否是预设
& & & & & & & & & & & & & & & & & & & & & & & & if(PrefabUtility.GetPrefabType(sceneObject) == PrefabType.PrefabInstance)
& & & & & & & & & & & & & & & & & & & & & & & & {
& & & & & & & & & & & & & & & & & & & & & & & & & & & & // 获取引用预设对象
& & & & & & & & & & & & & & & & & & & & & & & & & & & & Object prefabObject = EditorUtility.GetPrefabParent(sceneObject);
& & & & & & & & & & & & & & & & & & & & & & & & & & & & if(prefabObject != null)
& & & & & & & & & & & & & & & & & & & & & & & & & & & & {
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & XmlElement gameObjectXmlElement = xmlDocument.CreateElement(&gameObject&);
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & gameObjectXmlElement.SetAttribute(&objectName&, sceneObject.name);
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & gameObjectXmlElement.SetAttribute(&objectAssetURL&,&&prefabObject.name);
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & XmlElement transformXmlElement = xmlDocument.CreateElement(&transform&);
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & // 位置信息
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & XmlElement positionXmlElement = xmlDocument.CreateElement(&position&);
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & positionXmlElement.SetAttribute(&x&, sceneObject.transform.position.x.ToString());
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & positionXmlElement.SetAttribute(&y&, sceneObject.transform.position.y.ToString());
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & positionXmlElement.SetAttribute(&z&, sceneObject.transform.position.z.ToString());
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & // 旋转信息
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & XmlElement rotationXmlElement = xmlDocument.CreateElement(&rotation&);
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & rotationXmlElement.SetAttribute(&x&, sceneObject.transform.rotation.eulerAngles.x.ToString());
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & rotationXmlElement.SetAttribute(&y&, sceneObject.transform.rotation.eulerAngles.y.ToString());
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & rotationXmlElement.SetAttribute(&z&, sceneObject.transform.rotation.eulerAngles.z.ToString());
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & // 缩放信息
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & XmlElement scaleXmlElement = xmlDocument.CreateElement(&scale&);
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & scaleXmlElement.SetAttribute(&x&, sceneObject.transform.localScale.x.ToString());
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & scaleXmlElement.SetAttribute(&y&, sceneObject.transform.localScale.y.ToString());
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & scaleXmlElement.SetAttribute(&z&, sceneObject.transform.localScale.z.ToString());
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & transformXmlElement.AppendChild(positionXmlElement);
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & transformXmlElement.AppendChild(rotationXmlElement);
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & transformXmlElement.AppendChild(scaleXmlElement);& &
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & gameObjectXmlElement.AppendChild(transformXmlElement);
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & sceneXmlElement.AppendChild(gameObjectXmlElement);
& & & & & & & & & & & & & & & & & & & & & & & & & & & & }
& & & & & & & & & & & & & & & & & & & & & & & & }
& & & & & & & & & & & & & & & & & & & & }
& & & & & & & & & & & & & & & & }
& & & & & & & & & & & & & & & & rootXmlElement.AppendChild(sceneXmlElement);
& & & & & & & & & & & & & & & & xmlDocument.AppendChild(rootXmlElement);
& & & & & & & & & & & & & & & & // 保存场景数据
& & & & & & & & & & & & & & & & xmlDocument.Save(path);
& & & & & & & & & & & & & & & & // 刷新Project视图
& & & & & & & & & & & & & & & & AssetDatabase.Refresh();
& & & & & & & & & & & & }
& & & & & & & & }
& & & & }
}复制代码导出结果参考:&?xml version=&1.0& encoding=&utf-8&?&
&root&
&&&scene sceneName=&DongTaiJiaZaiTest&&
& & &gameObject objectName=&Box& objectAssetURL=&Box&&
& && &&transform&
& && &&&&position x=&-1.293883& y=&-0.07& z=&1.41& /&
& && &&&&rotation x=&270& y=&0& z=&0& /&
& && &&&&scale x=&1& y=&1& z=&1& /&
& && &&/transform&
& & &/gameObject&
& & &gameObject objectName=&meinv& objectAssetURL=&meinv&&
& && &&transform&
& && &&&&position x=&-1.75& y=&1.36& z=&11.28& /&
& && &&&&rotation x=&0& y=&97.43578& z=&0& /&
& && &&&&scale x=&1.09& y=&1.09& z=&1.09& /&
& && &&/transform&
& & &/gameObject&
& & &gameObject objectName=&Envirment& objectAssetURL=&Envirment&&
& && &&transform&
& && &&&&position x=&0& y=&0& z=&0& /&
& && &&&&rotation x=&0& y=&0& z=&0& /&
& && &&&&scale x=&1& y=&1& z=&1& /&
& && &&/transform&
& & &/gameObject&
& & &gameObject objectName=&RigidBodyFController& objectAssetURL=&RigidBodyFPSController&&
& && &&transform&
& && &&&&position x=&4.89& y=&1.46& z=&0.87& /&
& && &&&&rotation x=&0& y=&253.2478& z=&0& /&
& && &&&&scale x=&1& y=&1& z=&1& /&
& && &&/transform&
& & &/gameObject&
& & &gameObject objectName=&Sphere& objectAssetURL=&Sphere&&
& && &&transform&
& && &&&&position x=&-2.63& y=&3.28& z=&-3.95& /&
& && &&&&rotation x=&270& y=&0& z=&0& /&
& && &&&&scale x=&1& y=&1& z=&1& /&
& && &&/transform&
& & &/gameObject&
&&&/scene&
&/root&复制代码3.将预设打包为AssetBundle,并上传到服务器
& & & & 打包Assetbundle编辑器脚本:using UnityE
using System.C
using UnityE
public class Test : Editor
{
& & [MenuItem(&Custom Editor/Create AssetBunldes Main For Android&)]
& & static void CreateAssetBunldesMainForAndroid()
& & {
& && &&&//获取在Project视图中选择的所有游戏对象
& && &&&Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
& && &&&//遍历所有的游戏对象
& && &&&foreach (Object obj in SelectedAsset)
& && &&&{
& && && && &//本地测试:建议最后将Assetbundle放在StreamingAssets文件夹下,如果没有就创建一个,因为移动平台下只能读取这个路径
& && && && &//StreamingAssets是只读路径,不能写入
& && && && &//服务器下载:就不需要放在这里,服务器上客户端用www类进行下载。
& && && && &string targetPath = Application.dataPath + &/StreamingAssets/& + obj.name + &Android& + &.assetbundle&;
& && && && &if (BuildPipeline.BuildAssetBundle(obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.Android))
& && && && &{
& && && && && & Debug.Log(obj.name + &资源打包成功&);
& && && && &}
& && && && &else
& && && && &{
& && && && && & Debug.Log(obj.name + &资源打包失败&);
& && && && &}
& && &&&}
& && &&&//刷新编辑器
& && &&&AssetDatabase.Refresh();
& & }
& & [MenuItem(&Custom Editor/Create AssetBunldes Main For iPhone&)]
& & static void CreateAssetBunldesMainForiPhone()
& & {
& && &&&//获取在Project视图中选择的所有游戏对象
& && &&&Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
& && &&&//遍历所有的游戏对象
& && &&&foreach (Object obj in SelectedAsset)
& && &&&{
& && && && &string targetPath = Application.dataPath + &/StreamingAssets/& + obj.name + &iPhone& + &.assetbundle&;
& && && && &if (BuildPipeline.BuildAssetBundle(obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.iPhone))
& && && && &{
& && && && && & Debug.Log(obj.name + &资源打包成功&);
& && && && &}
& && && && &else
& && && && &{
& && && && && & Debug.Log(obj.name + &资源打包失败&);
& && && && &}
& && &&&}
& && &&&//刷新编辑器
& && &&&AssetDatabase.Refresh();
& & [MenuItem(&Custom Editor/Create AssetBunldes ALL For Android&)]
& & static void CreateAssetBunldesALLForAndroid()
& & {
& && &&&Caching.CleanCache();
& && &&&string Path = Application.dataPath + &/StreamingAssets/ALLAndroid.assetbundle&;
& && &&&Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
& && &&&foreach (Object obj in SelectedAsset)
& && &&&{
& && && && &Debug.Log(&Create AssetBunldes name :& + obj);
& && &&&}
& && &&&//这里注意第二个参数就行
& && &&&if (BuildPipeline.BuildAssetBundle(null, SelectedAsset, Path, BuildAssetBundleOptions.CollectDependencies, BuildTarget.Android))
& && &&&{
& && && && &AssetDatabase.Refresh();
& && &&&}
& && &&&else
& && &&&{
& && &&&}
& & }
& & [MenuItem(&Custom Editor/Create AssetBunldes ALL For iPhone&)]
& & static void CreateAssetBunldesALLForiPhone()
& & {
& && &&&Caching.CleanCache();
& && &&&string Path = Application.dataPath + &/StreamingAssets/ALLiPhone.assetbundle&;
& && &&&Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
& && &&&foreach (Object obj in SelectedAsset)
& && &&&{
& && && && &Debug.Log(&Create AssetBunldes name :& + obj);
& && &&&}
& && &&&//这里注意第二个参数就行
& && &&&if (BuildPipeline.BuildAssetBundle(null, SelectedAsset, Path, BuildAssetBundleOptions.CollectDependencies, BuildTarget.iPhone))
& && &&&{
& && && && &AssetDatabase.Refresh();
& && &&&}
& && &&&else
& && &&&{
& && &&&}
& & }
& & [MenuItem(&Custom Editor/Create Scene For Android&)]
& & static void CreateSceneALLForAndroid()
& & {
& && &&&Caching.CleanCache();
& && &&&Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
& && &&&foreach (Object obj in SelectedAsset)
& && &&&{
& && && && &string Path = Application.dataPath + &/StreamingAssets/& + obj.name + &Android& + &.&;
& && && && &string[] levels = { @&Assets/Scenes/ExportedScene/& + obj.name + &.unity& };
& && && && &BuildPipeline.BuildPlayer(levels, Path, BuildTarget.Android, BuildOptions.BuildAdditionalStreamedScenes);
& && && && &Debug.Log(&Craete Scene& + Path + &Complete!!&);
& && &&&}
& && &&&AssetDatabase.Refresh();
& & }
& & [MenuItem(&Custom Editor/Create Scene For iPhone&)]
& & static void CreateSceneALLForiPhone()
& & {
& && &&&Caching.CleanCache();
& && &&&Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
& && &&&foreach (Object obj in SelectedAsset)
& && &&&{
& && && && &string Path = Application.dataPath + &/StreamingAssets/& + obj.name + &iPhone& + &.unity3d&;
& && && && &string[] levels = { @&Assets/Scenes/ExportedScene/& + obj.name + &.unity& };
& && && && &BuildPipeline.BuildPlayer(levels, Path, BuildTarget.iPhone, BuildOptions.BuildAdditionalStreamedScenes);
& && && && &Debug.Log(&Craete Scene& + Path + &Complete!!&);
& && &&&}
& && &&&AssetDatabase.Refresh();
& & }
}复制代码4.将服务器的路径修改到XML配置文件中,并将XML上传到服务器
5.在Unity客户端下载XML并解析,下载并实例化对应的资源using UnityE
using System.C
using System.IO;
using System.Collections.G
using System.X
public class GetXMLDoc : MonoBehaviour
{
& & & & string fileP
& & & & public string XMLDocURL = &http://************.xml&;
& & & & void Awake ()
& & & & {
& & & & & & & & filePath = Application.persistentDataPath + &/XMLDoc1028.xml&;
& & & & & & & & if (File.Exists (filePath)) {
& & & & & & & & & & & & File.Delete (filePath);
& & & & & & & & }
& & & & & & & & WWW www = new WWW (XMLDocURL);
& & & & & & & & StartCoroutine (DownloadXMLDoc (www));
& & & & IEnumerator DownloadXMLDoc (WWW www)
& & & & {
& & & & & & & &
& & & & & & & & if (www.isDone) {
& & & & & & & & & & & & Debug.Log (&WWW is done&);
& & & & & & & & & & & & byte[] bts = www.
& & & & & & & & & & & & int length = bts.L
& & & & & & & & & & & & CreateXMLDoc (filePath, bts, length);
& & & & & & & & }
& & & & void CreateXMLDoc (string path, byte[] info, int lenth)
& & & & {
& & & & & & & & Debug.Log (&Start to create XML&);
& & & & & & & & S
& & & & & & & & FileInfo t = new FileInfo (path);
& & & & & & & & if (!t.Exists) {
& & & & & & & & & & & & sw = t.Create ();
& & & & & & & & } else {
& & & & & & & & & & & &
& & & & & & & & }
& & & & & & & & sw.Write (info, 0, lenth);
& & & & & & & & sw.Close ();
& & & & & & & & sw.Dispose ();
& & & & & & & & Debug.Log (&XML create sucess&);
& & & & & & & & LoadScene ();
//& && &&&下载完毕之后可以读取并进行实例化了
& & & & }
& & & & void LoadScene ()
& & & & {
& & & & & & & & Debug.Log (&开始加载&);
& & & & & & & & if (File.Exists (filePath)) {
& & & & & & & & & & & & XmlDocument xmlDoc = new XmlDocument ();
& & & & & & & & & & & & xmlDoc.Load (filePath);
& & & & & & & & & & & & XmlNodeList nodeList = xmlDoc.SelectSingleNode (&root&).ChildN
& & & & & & & & & & & & foreach (XmlElement scene&&in nodeList) {
& & & & & & & & & & & & & & & & //因为我的XML是把所有游戏对象全部导出, 所以这里判断一下只解析需要的场景中的游戏对象
& & & & & & & & & & & & & & & & //JSON和它的原理类似
//& && && && && & if (!scene.GetAttribute(&name&).Equals(&Assets/StarTrooper.unity&))
//& && && && && & {
//& && && && && && &&&
//& && && && && & }
& & & & & & & & & & & & & & & & foreach (XmlElement gameObjects in scene.ChildNodes) {
//& && && && && && &&&取得资源地址
& & & & & & & & & & & & & & & & & & & & string assetRUL = gameObjects.GetAttribute (&objectAssetURL&);
& & & & & & & & & & & & & & & & & & & & string assetName = gameObjects.GetAttribute (&objectName&);
& & & & & & & & & & & & & & & & & & & & Vector3 pos = Vector3.
& & & & & & & & & & & & & & & & & & & & Vector3 rot = Vector3.
& & & & & & & & & & & & & & & & & & & & Vector3 sca = Vector3.
& & & & & & & & & & & & & & & & & & & & foreach (XmlElement transform in gameObjects.ChildNodes) {
& & & & & & & & & & & & & & & & & & & & & & & & foreach (XmlElement prs in transform.ChildNodes) {
& & & & & & & & & & & & & & & & & & & & & & & & & & & & if (prs.Name == &position&) {
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & pos.x = float.Parse(prs.GetAttribute(&x&));
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & pos.y = float.Parse(prs.GetAttribute(&y&));
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & pos.z = float.Parse(prs.GetAttribute(&z&));
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & foreach (XmlElement position in prs.ChildNodes) {
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & switch (position.Name) {
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & case &x&:
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & pos.x = float.Parse (position.InnerText);
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & case &y&:
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & pos.y = float.Parse (position.InnerText);
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & case &z&:
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & pos.z = float.Parse (position.InnerText);
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & }
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & }
& & & & & & & & & & & & & & & & & & & & & & & & & & & & } else if (prs.Name == &rotation&) {
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & rot.x = float.Parse (prs.GetAttribute(&x&));
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & rot.y = float.Parse(prs.GetAttribute(&y&));
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & rot.z = float.Parse(prs.GetAttribute(&z&));
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & foreach (XmlElement rotation in prs.ChildNodes) {
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & switch (rotation.Name) {
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & case &x&:
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & rot.x = float.Parse (rotation.InnerText);
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & case &y&:
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & rot.y = float.Parse (rotation.InnerText);
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & case &z&:
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & rot.z = float.Parse (rotation.InnerText);
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & }
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & }
& & & & & & & & & & & & & & & & & & & & & & & & & & & & } else if (prs.Name == &scale&) {
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & sca.x = float.Parse (prs.GetAttribute(&x&));
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & sca.y = float.Parse(prs.GetAttribute(&y&));
& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & sca.z = float.Parse(prs.GetAttribute(&z&));
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & foreach (XmlElement scale in prs.ChildNodes) {
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & switch (scale.Name) {
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & case &x&:
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & sca.x = float.Parse (scale.InnerText);
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & case &y&:
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & sca.y = float.Parse (scale.InnerText);
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & case &z&:
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & sca.z = float.Parse (scale.InnerText);
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & }
//& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & }
& & & & & & & & & & & & & & & & & & & & & & & & & & & & }
& & & & & & & & & & & & & & & & & & & & & & & & }
& & & & & & & & & & & & & & & & & & & & & & & & Debug.Log (&准备下载:& + assetRUL);
//& && && && && && && && &开始下载并实例化对象
& & & & & & & & & & & & & & & & & & & & & & & & Debug.Log (assetName + &:pos=& + pos);
& & & & & & & & & & & & & & & & & & & & & & & & Debug.Log (assetName + &:rot=& + pos);
& & & & & & & & & & & & & & & & & & & & & & & & Debug.Log (assetName + &:sca=& + pos);
& & & & & & & & & & & & & & & & & & & & & & & & StartCoroutine (DownloadAsset (new WWW (assetRUL), pos, rot, sca));
& & & & & & & & & & & & & & & & & & & & & & & & //拿到 旋转 缩放 平移 以后克隆新游戏对象
//& && && && && && && && &GameObject ob = (GameObject)Instantiate(Resources.Load(asset), pos, Quaternion.Euler(rot));
//& && && && && && && && &ob.transform.localScale =
& && && && && && &&&
& & & & & & & & & & & & & & & & & & & & }
& & & & & & & & & & & & & & & & }
& & & & & & & & & & & & }
& & & & & & & & }
& & & & IEnumerator DownloadAsset (WWW www, Vector3 pos, Vector3 rot, Vector3 sca)
& & & & {
& & & & & & & &
& & & & & & & & if (www.isDone) {
//& && && && &yield return Instantiate(www.assetBundle.mainAsset,pos,pos,Quaternion.Euler(rot));
& & & & & & & & & & & & GameObject ob = (GameObject)Instantiate (www.assetBundle.mainAsset, pos, Quaternion.Euler (rot));
& & & & & & & & & & & & ob.transform.localScale =
& & & & & & & & & & & & www.assetBundle.Unload (false);
& & & & & & & & }
& & & & }
}
复制代码
最后登录注册时间阅读权限70积分2411
中级设计师, 积分 2411, 距离下一级还需 2589 积分
纳金币1245 精华3
感谢分享,值得借鉴
站长推荐 /1
纳金名模第151期地毯3d模型:数量100个,大小为2.15GB。完全免费下载哦!这样的福利我和我的小伙伴们都惊呆了!!下载地址:
Powered by - X2.5
Narkii Inc.

我要回帖

更多关于 unity 场景切换 的文章

 

随机推荐