父Prefab与子prefab问题
PrefabRevolution
原文:http://framebunker.com/blog/poor-mans-nested-prefabs/ (溜还是老外溜啊
)
有些时候需要在Prefab里预制一个子prefab,但unity只会默认父prefab,一旦预制了prefab就会与其内部的prefab失去关联;
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
}
PrefabInstance
类能解决这个问题,用法:比如有两个prefab,PrefabFarent和PrefabSon,只需要把PrefabInstance
类挂到PrefabFarent上,把PrefabSon拖到prefab里就行。
这样无论怎么改变PrefabSon,当实例化PrefabFarent时都能得到最新改变了的PrefabSon
父Prefab与子prefab问题的更多相关文章
- Unity3D研究院之Prefab里面的Prefab关联问题
最近在做UI部分中遇到了这样的问题,就是Prefab里面预制了Prefab.可是在Unity里面一旦Prefab预制了Prefab那么内部的Prefab就失去关联.导致与如果要改内部的Prefab需要 ...
- JavaScript从父页面获取子页面的值(子页面又如何访问父页面)
之前还真没做过类似的东西,,top页面获取子页面的document.. 在百度搜了下即找到这个东东,还好,能用. 主要就是使用 contentWindow方法,获取子页面的所有document,再做处 ...
- Jquery父页面和子页面的相互操作
//父页面调用子页面Add函数 $("iframe")[0].contentWindow.Add() //父页面对子页面Id为Sava的Dom元素执行一次单击操作 $(" ...
- Caliburn.Micro 关闭父窗体打开子窗体
比如我们在做登录的时候需要关闭父窗体打开子窗体.使用Caliburn.Micro它的时候我们关闭登录窗口的时候主页面也会关闭. 解决方法就是在登录页面的CS里面写 IndexView iv = new ...
- 父容器根据子容器高度自适应:设置父容器 height:100%;overflow:hidden;
父容器根据子容器高度自适应:设置父容器 height:100%;overflow:hidden;
- 父元素与子元素之间的margin-top问题
父元素的盒子包含一个子元素盒子,给子元素盒子一个垂直外边距margin-top,父元素盒子也会往下走margin-top的值,而子元素和父元素的边距则没有发生变化. html代码: <div c ...
- Unity3D研究院之Prefab里面的Prefab关联问题(转)
转自http://www.xuanyusong.com/archives/3042 最近在做UI部分中遇到了这样的问题,就是Prefab里面预制了Prefab.可是在Unity里面一旦Prefab预制 ...
- HTML 父元素与子元素之间的margin-top问题
问题: 父元素的盒子包含一个子元素盒子,给子元素盒子一个垂直外边距margin-top,父元素盒子也会往下走margin-top的值,而子元素和父元素的边距则没有发生变化. 代码如下: <div ...
- artdialog4.1.7 中父页面给子页面传值
artdialog4.1.7中父页面给子页面传值时看了一些网友的解决方法: 在父页面声明全局变量 var returnValue=“ ”,子页面用art.dialog.opener.returnVal ...
随机推荐
- Java学习日记 集合
一.接口Map<K,V>1.V put(K key, V value)2.int size()3.public class HashMap<K, V> implements M ...
- UVALive 6198 A Terribly Grimm Problem
题目大意是 给出L,H 10^10范围 为[L, H]这个连续的整数区间寻找一个序列. 序列的长度要跟[L, H]一样 然后序列中的数都是素数,并且互不相同 并且序列中第i个数 要求是L + ...
- python成长笔记
正则表达式 1. 择一匹配:管道符号(|),表示“从多个模式中选择其一”.例:at|home à at.home 2. 点号匹配除了换行符以外的任何字符 3. 边界匹配:\b匹配一个 ...
- Java中Timer的用法--转载之网络
用法很简单,new一个timer,然后写一个timertask的子类即可. import java.util.Timer; import java.util.TimerTask; public cla ...
- IE浏览器下面要实现滤镜(transparent),必须要加filter
遇到的问题是: ie9下面的a标签样式是background-color:transparent;导致链接失效,点不动.这个问题跟IE9及其以下a标签链接加 background-color:tran ...
- JS 实现显示和隐藏div(以百度地图为例)
主要参考的文章:https://my.oschina.net/xsh1208/blog/215811,https://zhidao.baidu.com/question/568774688.html ...
- java—— 笔记链接
深入Java关键字null 链接: http://tieba.baidu.com/f?kz=510289524 颜色代码 链接: http://wenku.baidu.com/link?url= ...
- 解决本地软件链接不上虚拟机mysql 的问题:grant all privileges on *.* to 'root'@'%' identified by 'nsfocus'
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' -> IDENTIFIED BY 'some_pass' WITH ...
- swift学习资料初探
1. http://code.csdn.net/news/2820075
- 安卓(android)建立项目时失败,出现Android Manifest.xml file missing几种解决方法?(总结中)
安卓(android)建立项目时失败.出现AndroidManifest.xml file missing几种解决方法?(总结中) Eclipse新建项目.遇到这种问题.注意例如以下: 1.文件名称最 ...