屌丝手动版

One of the things we’re sorely missing from Unity is nested prefabs. So we rolled this little script to help us out – and figured other people could benefit as well.

You just add this script to a gameobject, and point the prefab field at the prefab you want instantiated.

This will render the prefab in the scene view while editing, and at bake time it will copy the objects into the scene so your gamecode can just work. It doesn’t allow overriding properties, and only previews the meshes, but we’ve been able to use it a lot for building props: We have a Gameobject that contains all the logic for a prop (e.g. definitions of cover points for a crate) – and then uses this script to pull in the actual FBX file that has the graphics. Now our graphic artist can just modify that and everything works.

You can also reference other prefabs, and nest these PrefabInstance scripts. While not as cool as a real system, it pretty much has solved our use cases.

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 = ; 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 (,,,)); }
void OnDrawGizmosSelected () { DrawGizmos (new Color (,,,.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(-)]
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
}

It uses the same principle as our IBakeable interface, but we want this to run before all the other IBakeable functions (so all objects are instantiated before we try to run others)

 
半自动版
http://u3d.as/content/bento-studio/nested-prefab/2Jz
链接: http://pan.baidu.com/s/1kT9LagZ 密码: igly
 
据说是全自动版
http://u3d.as/content/databox-games/prefabs-in-prefabs-plugin/2Px
 

NestedPreb的更多相关文章

随机推荐

  1. hdu 4788

    #include<stdio.h> #include<math.h> int main() { int a; double d; char s],ch; for;i++) d; ...

  2. 【HDOJ6148】Valley Numer(数位DP)

    题意: 1≤T≤200 ● 1≤length(N)≤100 思路: 设f[i,j,k,l]为第i位为j,前i位是否贴上限(0/1),递减或递增(0/1)方案数 g[i,j,k]为不到n位,第i位为j, ...

  3. openjudge6252 带通配符的字符串匹配

    描述 通配符是一类键盘字符,当我们不知道真正字符或者不想键入完整名字时,常常使用通配符代替一个或多个真正字符.通配符有问号(?)和星号(*)等,其中,“?”可以代替一个字符,而“*”可以代替零个或多个 ...

  4. Webstrom打开太慢

    让webstrom将安装包目录屏蔽,settings-搜索file types-在ignore file and folders加入node_modules目录,操作方式如下:

  5. Linux系统备份还原工具3(使用Clonezilla/再生龙对硬盘进行镜像和克隆,类似于Ghost)

    说明:经过实验验证,再生龙主要是适合在本机还原原大小的分区,不适合将镜像备份还原到不同大小分区,期间可能有很多莫名奇妙的问题出现.硬盘对拷和PXE网刻这些没发现什么不好.如果要还原到别的电脑镜像制作时 ...

  6. Delphi 2007 的重构功能

    Move 移动 1.将选定的静态函数从一个类移动到另一个类 2.将选中的类或接口移动到其他单元 Extract Interface 抽取接口 将选定的函数抽取到一个新的接口中 Extract Supe ...

  7. 【Unix编程】进程间通信(IPC)

    进程间通信(IPC,InterProcess Communication)是指在不同进程之间传播或交换信息.IPC的方式通常有管道(包括无名管道和命名管道).消息队列.信号量.共享存储.Socket. ...

  8. POJ 3342 Party at Hali-Bula (树形dp 树的最大独立集 判多解 好题)

    Party at Hali-Bula Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5660   Accepted: 202 ...

  9. 分析Linux内核的启动过程

    第一章 环境 Ubuntu 14.10 Linux Kernel 3.18.6 第二章 代码及调试过程 环境搭建与内核准备: cd ~/LinuxKernel/ wget https://www.ke ...

  10. PHP在浏览器上跟踪调试的方法以及使用ChromePhp、FirePHP的简介

    之前用ThinkPHP时发现有个 trace 函数能够跟踪调试,感觉非常有意思.网上搜索了下类似的东西.发现了 ChromePhp ,曾经没想过这样来调试 PHP 程序.感觉非常方便,非常实用. Th ...