unity3d 脚本改变unity 预制体件的属性是永久的吗

二次元同好交流新大陆
扫码下载App
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
阅读(5953)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'Unity3D中的预制件(Prefab)的创建和使用说明',
blogAbstract:'转自:'
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}Unity prefab实例属性不随预制源改变的问题 - 兜里
作者:切梦刀
热度:5159℃
今天遇到一个修改了prefab源的属性后,prefab实例属性不随着改变的问题。
操作步骤:
在Unity Editor中创建prefab对象Player;
托一个Player的实例p1出来;
修改Player预制对象的z属性;
p1的z属性未跟着变;
在Unity组件手册中这么说的:
Inheritance 继承
Inheritance means that whenever the source Prefab changes, those changes are applied to all linked GameObjects. For example, if you add a new script to a Prefab, all of the linked GameObjects will instantly contain the script as well. However, it is possible to change the properties of a single instance while keeping the link intact. Simply change any property of a prefab instance, and watch as the variable name becomes bold. The variable is now overridden. All overridden properties will not be affected by changes in the source Prefab.
继承是指当预置源发生变化,这些变化将应用于所有已链接的游戏对象。例如,如果添加一个新的脚本到预置,所有已链接的游戏对象都将立刻包含该脚本。但是,它有可能改变一个单独实例的属性,同时保持链接。改变任何一个预置实例的属性,可以看到变量名称变为粗体,现在该变量可以被重写,所有的重写属性不会影响预置源的变化。
那么,p1.z应该随着Player.z改变才对。
与同事讨论并多次测试后发现,Prefab源的位置Position和旋转Rotation不会被Prefab实例继承,而缩放Scale可被继承。
也就是,修改Prefab源的Position和Rotation属性,Prefab实例不会随着改变。
添加新评论
:很酷的域名
:一个bug,改好了
:磊子,你是穿越回来的不?今天是,你上面2016-0...
:磊子,你是穿越回来的不?今天才,你上面2016-0...
:没空写东西,有空时又不想写
:你怎么也这么久没有更新了
:变成什么?
:去掉BOM头变成,咋让他不变貌似说偏了,假设预制里面,有个脚本,中可以设置一个全局的变量。那么会出现这种情况。-Unity3D-论坛-U3D在线
Unity3D在线
当前位置: ->
-> 貌似说偏了,假设预制里面,有个脚本,中可以设置一个全局的变量。那么会出现这种情况。
Unity3D 论坛 > 貌似说偏了,假设预制里面,有个脚本,中可以设置一个全局的变量。那么会出现这种情况。围观:497 | 回复:22
免责声明:本站所有内容来源于互联网,如果本站部分内容侵犯您的权益,请您告知,站长会立即处理
Copyright Unity3D在线 All Rights Reserved.Theme by U3dOL 备案:京ICP备号-4 |SiteMap |网站地图 |百度统计| 联系我们Unity3D随机产生10个预制件 重点是怎样让他们两两相同 用C#写_百度知道
Unity3D随机产生10个预制件 重点是怎样让他们两两相同 用C#写
我有更好的答案
溶了以后放入相同的磨具制造
其他类似问题
为您推荐:
unity3d的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁程序写累了,就来玩玩酷跑小游戏吧,嘿嘿。
雨松MOMO送你一首歌曲,嘿嘿。
Unity3D研究院之Prefab里面的Prefab关联问题
Unity3D研究院之Prefab里面的Prefab关联问题
围观21461次
编辑日期: 字体:
最近造了个轮子可以批量替换prefab里的prefab,欢迎大家测试~
最近在做UI部分中遇到了这样的问题,就是Prefab里面预制了Prefab。可是在Unity里面一旦Prefab预制了Prefab那么内部的Prefab就失去关联。导致与如果要改内部的Prefab需要把所有引用的地方全部改一遍。今天在逛国外网站看到了一个老外的思路,原文在这里
下面直接上代码
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
using UnityEngine;#if UNITY_EDITORusing UnityEditor;using UnityEditor.Callbacks;#endifusing 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)
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)
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)
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)
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不操作脚本。
最近造了个轮子可以批量替换prefab里的prefab,欢迎大家测试~
如果有更好的方法欢迎各位朋友在下面给我留言,谢谢。
本文固定链接:
转载请注明:
雨松MOMO提醒您:亲,如果您觉得本文不错,快快将这篇文章分享出去吧 。另外请点击网站顶部彩色广告或者捐赠支持本站发展,谢谢!
作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
如果您愿意花10块钱请我喝一杯咖啡的话,请用手机扫描二维码即可通过支付宝直接向我捐款哦。
您可能还会对这些文章感兴趣!}

我要回帖

更多关于 unity 脱离预制体 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信