最近在做UI部分中遇到了这样的问题,就是Prefab里面预制了Prefab。可是在Unity里面一旦Prefab预制了Prefab那么内部的Prefab就失去关联。导致与如果要改内部的Prefab需要把所有引用的地方全部改一遍。今天在逛国外网站看到了一个老外的思路,原文在这里 http://framebunker.com/blog/poor-mans-nested-prefabs/
下面直接上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.Callbacks;
#endif
using System.Collections.Generic;
[ExecuteInEditMode]
public class PrefabInstance : MonoBehaviour
{
public GameObject prefab;
#if UNITY_EDITOR
// Struct of all components. Used for edit-time visualization and gizmo drawing
public struct Thingy {
public Mesh mesh;
public Matrix4x4 matrix;
public List<Material> materials;
}
[System.NonSerializedAttribute] public List<Thingy> things = new List<Thingy> ();
void OnValidate () {
things.Clear();
if (enabled)
Rebuild (prefab, Matrix4x4.identity);
}
void OnEnable () {
things.Clear();
if (enabled)
Rebuild (prefab, Matrix4x4.identity);
}
void Rebuild (GameObject source, Matrix4x4 inMatrix) {
if (!source)
return;
Matrix4x4 baseMat = inMatrix * Matrix4x4.TRS (-source.transform.position, Quaternion.identity, Vector3.one);
foreach (MeshRenderer mr in source.GetComponentsInChildren(typeof (Renderer), true))
{
things.Add(new Thingy () {
mesh = mr.GetComponent<MeshFilter>().sharedMesh,
matrix = baseMat * mr.transform.localToWorldMatrix,
materials = new List<Material> (mr.sharedMaterials)
});
}
foreach (PrefabInstance pi in source.GetComponentsInChildren(typeof (PrefabInstance), true))
{
if (pi.enabled && pi.gameObject.activeSelf)
Rebuild (pi.prefab, baseMat * pi.transform.localToWorldMatrix);
}
}
// Editor-time-only update: Draw the meshes so we can see the objects in the scene view
void Update () {
if (EditorApplication.isPlaying)
return;
Matrix4x4 mat = transform.localToWorldMatrix;
foreach (Thingy t in things)
for (int i = 0; i < t.materials.Count; i++)
Graphics.DrawMesh (t.mesh, mat * t.matrix, t.materials[i], gameObject.layer, null, i);
}
// Picking logic: Since we don't have gizmos.drawmesh, draw a bounding cube around each thingy
void OnDrawGizmos () { DrawGizmos (new Color (0,0,0,0)); }
void OnDrawGizmosSelected () { DrawGizmos (new Color (0,0,1,.2f)); }
void DrawGizmos (Color col) {
if (EditorApplication.isPlaying)
return;
Gizmos.color = col;
Matrix4x4 mat = transform.localToWorldMatrix;
foreach (Thingy t in things)
{
Gizmos.matrix = mat * t.matrix;
Gizmos.DrawCube(t.mesh.bounds.center, t.mesh.bounds.size);
}
}
// Baking stuff: Copy in all the referenced objects into the scene on play or build
[PostProcessScene(-2)]
public static void OnPostprocessScene() {
foreach (PrefabInstance pi in UnityEngine.Object.FindObjectsOfType (typeof (PrefabInstance)))
     BakeInstance (pi);
}
public static void BakeInstance (PrefabInstance pi) {
if(!pi.prefab || !pi.enabled)
return;
pi.enabled = false;
GameObject go = PrefabUtility.InstantiatePrefab(pi.prefab) as GameObject;
Quaternion rot = go.transform.localRotation;
Vector3 scale = go.transform.localScale;
go.transform.parent = pi.transform;
go.transform.localPosition = Vector3.zero;
go.transform.localScale = scale;
go.transform.localRotation = rot;
pi.prefab = null;
foreach (PrefabInstance childPi in go.GetComponentsInChildren<PrefabInstance>())
BakeInstance (childPi);
}
#endif
}

用法比较简单,比如我有两个Prefab,inside嵌入在Big里面。如下图所示,把PrefabInstance脚本挂在Big上,然后把inside拖入下方。

OK 无论怎么修改inside这个Prefab,当实例化Big的时候都能得到最新修改的Inside这个Prefab。

持续思考:

界面预览问题,就是我在布界面的时候,我需要把子集Prefab界面控件拖进来预览效果。如果用上述思路UI的Prefab就必须通过脚本自动生成。一份是预览用的也就是不需要脚本的,一份是只带脚本运行时动态生成的。在处理自动生成UIPrefab的时候可以利用tag 比如像这种需要内嵌的Prefab标记一个特殊的tag,在Editor下完成Prefab的导出。另外布界面的时候不需要绑定脚本,而上述脚本的绑定也应该由Editor导出Prefab的时候完成。

总之一切布界面的时候只操作Prefab不操作脚本。

如果有更好的方法欢迎各位朋友在下面给我留言,谢谢。

  • 原文链接: http://www.xuanyusong.com/archives/3042

转载于:https://www.cnblogs.com/dabiaoge/p/4583257.html

Unity3D研究院之Prefab里面的Prefab关联问题相关推荐

  1. Unity3D研究院之Unity中连接本地或局域网MySQL数据库

    用户名 Email 游戏蛮牛 手机端 开启辅助访问 腾讯QQ 立即注册 登录 用户名 自动登录  找回密码 密码 登录  注册帐号 [Unity5.X版本开始预售啦!] 扫一扫,访问微社区 </ ...

  2. [unity3D]什么是预制体(Prefab)?如何制作预制体?如何导出预制体?预制体变体是什么?

    [unity3D]什么是预制体(Prefab)?如何制作预制体?如何导出预制体?预制体变体是什么? 0.预制体概念 1.制作预制体 2.导出预制体 3.预制体变体 0.预制体概念 将物体转成预制体之后 ...

  3. unity5.4.3p2里面的AssetBundle打包流程

    unity5.4.3p2里面的AssetBundle打包流程,相比之前unity4.x的打包简单了许多,Unity4.X中打包的时候需要自己去管理依赖关系,各种BuildPipeline.PushAs ...

  4. 转:Unity3D研究院之提取游戏资源的三个工具支持Unity5(八十四)

    这两天无意间又发现了两个提取Unity游戏资源的工具,这会儿刚好有时间我就码点字总结一下. 一.disunity 因为之前写过了所以这里就不介绍了 .Unity3D研究院之mac上从.ipa中提取un ...

  5. Netty里面的Boss和Worker【Server篇】

    转载地址:https://my.oschina.net/bieber/blog/406799 最近在总结Dubbo关于Netty通信方面的实现,于是也就借此机会深入体会了一下Netty.一般启动Net ...

  6. OAuth2.0 里面的 state 参数是干什么的?

    OAuth2.0 里面的 state 参数是干什么的? 1.OAuth 2.0 的四种方式 授权码 常见于个人用户第三方登录,比如通过微信.QQ.钉钉登录第三方应用.获取临时的access_token ...

  7. Unity3D研究院之挥动武器产生的剑痕特效(四十七)

    雨松MOMO 最近超级忙,好久没写东西啦.前几天学习了一下如何实现剑痕特效.用公司的模型写例子不太好,所以我还是用以前那个小牛头人 嘿嘿.如下图所示,你懂得蛤蛤. 目前已知3种方法可以做这种剑痕特效 ...

  8. 关于JS里面的Call Stack and Heap

    关于JS里面的Call Stack and Heap Abstract What is the Call Stack and Heap in JS What is the difference bet ...

  9. PyTorch里面的torch.nn.Parameter()

    在刷官方Tutorial的时候发现了一个用法self.v = torch.nn.Parameter(torch.FloatTensor(hidden_size)),看了官方教程里面的解释也是云里雾里, ...

  10. servlet里面的context,定义属性值的事情!

    servlet里面的context,定义属性值. 在尚未指定属性值的时候.不管谁调用,都是NULL. 但是,一旦指定了属性值.那么,再次调用就是之间指定的数值啦! 而且,是象mapping一样存储对象 ...

最新文章

  1. 算法学习:计算几何旋转卡壳
  2. 运行android模拟器,emulator: ERROR: x86 emulation currently requires hardware acceleration!
  3. Android 圆角Button
  4. kotlin编写后台_在Kotlin编写图书馆的提示
  5. CF940D Alena And The Heater
  6. 腾讯也有“神盾局”?秀出“技术肌肉”就靠TA了……
  7. 中国女排代言作业帮直播课,作业帮累计用户已超8亿
  8. 手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)【转】
  9. MyEclipse取消验证Js的两种方法
  10. ASP.NET之Application、Session和Cookie的差别
  11. 树链剖分(bzoj 1036: [ZJOI2008]树的统计Count)
  12. 解决 git branch -a 无法全部显示远程的分支,只显示master分支
  13. 页面三个txt加载联动省市县的代码,类似淘宝的收货地址的布局
  14. windows中使用docker构建镜像
  15. Oracle in 不能超过1000的解决方案
  16. 最适合养老的20座城市
  17. 网络传输介质和常见的设备
  18. 智慧城市智能化建设发展现状及展望
  19. Cocos2d-x随记(2)-精灵移动
  20. 聊聊接口性能优化的11个小技巧

热门文章

  1. PHP如果某商品下的所有货品库存都为0,则下架该商品
  2. linux扩容系统盘分区,系统盘扩容 扩展分区与文件系统_Linux系统盘
  3. 【无标题】梦笔记2022-02-20
  4. 编码基本功:工作中,大多数人不会举一反三
  5. 体验迟到:果然是魔鬼定律,一路上多种障碍
  6. 软件基本功:做自说明的测试文档,
  7. 无法解析的外部符号:GetWindowThreadProcessId/EnumWindow
  8. 同事去了友司,没什么可怕的
  9. Windows下实现gettimeofday()函数
  10. 市场经济中,大家都象防贼一样保留证据,以用于欠薪仲裁