繁体   English   中英

如何让游戏对象将更改应用于脚本中的预制件

[英]how do i get a game object to apply changes to a prefab in script

使用Unity 3D编辑器。 我有一个物体在与其他物体碰撞时会改变颜色,这一切都有效,但是当改变场景时,球会恢复到默认颜色。 所以我在改变颜色时需要球来通过脚本更新预制件,这样其他场景中对象的其他实例也会改变颜色。 这是我为变色编写的代码:

    using UnityEngine;
using System.Collections;

public class colourpicker : MonoBehaviour {

    public GameObject whiteblock;
    public GameObject limegreentintblock;
    public GameObject cyantintblock;
    public GameObject redtintblock;
    public GameObject yellowtintblock;
    public GameObject player;

    public Material limegreentint;
    public Material redtint;
    public Material cyantint;
    public Material yellowtint;
    public Material white;

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject == whiteblock) 
        {
            renderer.material = white;
        }
        else if (other.gameObject == redtintblock) 
        {
            renderer.material = redtint;
        }
        else if (other.gameObject == limegreentintblock) 
        {
            renderer.material = limegreentint;
        }
        else if (other.gameObject == cyantintblock) 
        {
            renderer.material = cyantint;
        }
        else if (other.gameObject == yellowtintblock) 
        {
            renderer.material = yellowtint;
        }
    }
}

谢谢你提前帮助:)

如果我是你,我会创建一个带有空游戏对象的静态类,您可以在其中保存对象颜色的状态。 在Ball的Initialize部分中,您应该检查此静态类的变量(必须是静态的)是否与Initial状态不同,然后将静态变量的颜色分配给Ball对象。 这个LINK教你如何实现它(你必须改变一些东西来做你想要的东西)

编辑

在静态类中,您可以创建类似的东西

public static Material BallMaterial = null;   // or you can assign a Material that will act as InitialState

之后,在您的代码中,当您将新材质指定给球时,您还将新材质指定给BallMaterial ,然后在加载新场景时检查BallMaterial是否具有与其InitialState不同的值(或null )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM