屌丝手动版

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. STM32F407 串口通信实验 第26节 个人笔记

    前言 这篇笔记对应正点原子STM32F407探索者 ,教学视频第26节,网址如下: https://ke.qq.com/webcourse/index.html#cid=279403&term ...

  2. ajax一个很好的加载效果

    推荐一个常用的jquery加载效果插件: 要引入这个插件的css和js: <link href="<%=path %>/css/showLoading.css" ...

  3. [BZOJ2667][cqoi2012]模拟工厂

    [BZOJ2667][cqoi2012]模拟工厂 试题描述 有一个称为“模拟工厂”的游戏是这样的:在时刻0,工厂的生产力等于1.在每个时刻,你可以提高生产力或者生产商品.如果选择提高生产力,在下一个时 ...

  4. acm 一年总结

    首先是大一的一段简短历史,和其他人不太一样,刚上大一的我等于是刚刚接触电脑,开始下载程序啦,安装系统了,电脑出个小问题啦自己都不会解决,然后大一还开了一门叫做c语言的课程,顿时傻逼了,当时也不用功,大 ...

  5. 【进击后端】mongodb入门

    环境:ubuntu 重要字:数据库,集合,记录,字段 api地址:https://docs.mongodb.com/manual/reference/method/db.collection.upda ...

  6. 远程调试 Android 设备使用入门(谷歌翻译版)

    移动前端调试方案(Android + Chrome 实现远程调试) 目录 要求 第 1 步:发现您的 Android 设备 第 2 步:从您的开发计算机调试 Android 设备上的内容. 更多操作: ...

  7. Access restriction: The type 'JPEGImageWriter' is not API

    报错: Access restriction: The type 'JPEGImageWriter' is not API due to restriction on required library ...

  8. python函数值传递还是引用传递

    c/c++中有值传递引用传递的区别.但是python中是值传递还是引用传递呢?首先看python中对变量的定义 "python中变量是指向某个内存的, 而内存中的内容是不可变的." ...

  9. 关于python内存管理里的引用计数算法和标记-清楚算法的讨论

    先记录于此,后续有时间再深究吧: 1.https://www.zhihu.com/question/33529443 2.http://patshaughnessy.net/2013/10/30/ge ...

  10. symfony 数据库中文乱码

    这个问题 是由于编辑器没有设置utf8格式造成的,当然config里也要设置utf8 解决方法:编辑器设置utf8,重启 doctrine: dbal: driver: pdo_mysql host: ...