最近在做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
;
#if UNITY_EDITOR
;
;
#endif
;
 
]
MonoBehaviour
{
;
 
#if UNITY_EDITOR
// Struct of all components. Used for edit-time visualization and gizmo drawing
{
;
;
;
}
 
;
 
{
;
)
;
}
 
{
;
)
;
}
 
{
)
;
 
;
 
)
{
{
,
,
)
;
}
 
)
{
)
;
}
}
 
// Editor-time-only update: Draw the meshes so we can see the objects in the scene view
{
)
;
;
)
)
;
}
 
// Picking logic: Since we don't have gizmos.drawmesh, draw a bounding cube around each thingy
}
}
{
)
;
;
;
)
{
;
;
}
}
 
// Baking stuff: Copy in all the referenced objects into the scene on play or build
]
{
)
;
}
 
{
)
;
;
;
;
;
;
;
;
;
;
)
;
}
 
#endif
}

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

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

 

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

持续思考:

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

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

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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-10
  • 2021-07-14
  • 2021-11-28
  • 2021-05-22
  • 2021-11-14
  • 2021-07-31
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-18
  • 2022-12-23
  • 2021-12-07
  • 2021-07-03
  • 2022-12-23
相关资源
相似解决方案