【问题标题】:How do I replace the current gameObject with a new one while saving the functionality from the old one?如何在保存旧游戏对象的功能的同时用新游戏对象替换当前游戏对象?
【发布时间】:2019-01-13 16:41:32
【问题描述】:

我试图让用户选择将当前游戏对象(立方体)更改为新形状(球体),但是,我不知道如何携带我为立方体到球体。

我已经设法销毁旧的立方体并将其替换为球体,但该球体似乎没有任何用于立方体的按键控件。我有一个想法,将新游戏对象的方法设置为我用于立方体的方法(即 newSphere.transform.rotation() = this.transform.rotation();),但它似乎不起作用。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class cubeControls : MonoBehaviour
{
    // Constants for object rotation
    public float moveSpeed = 80.0F;
    public float turnSpeed = 100.0F;

    // Initial scale of the original cube
    public static Vector3 initscale = Vector3.one;

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        /* Changing the position of the object
         * 
         */

        // Moving the object right
        if (Input.GetKey(KeyCode.D))
        {
            transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
        }

        // Moving the object left
        if (Input.GetKey(KeyCode.A))
        {
            transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
        }

        /* Changing the rotation of the object
        * 
        */

        // Rotating the cube to the right
        if (Input.GetKey(KeyCode.RightArrow))
        {
            transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
        }

        // Rotating the cube to the left
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            transform.Rotate(Vector3.down, turnSpeed * Time.deltaTime);
        }

        // Saving the current rendered material
        Renderer rend = GetComponent<Renderer>();

        /* Changing the scale of the object
        * 
        */

        // Double the size of the cube
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            transform.localScale += new Vector3(2F, 2F, 2F);
        }

        /* Changing the color via key presses
         * 
         */

        if (Input.GetKeyDown(KeyCode.R))
        {
            rend.material.SetColor("_Color", Color.red);
        }

        // Changing to sphere
        if (Input.GetKeyDown(KeyCode.X))
        {
            GameObject newSphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            Destroy(this.gameObject);
        }
    }
}

【问题讨论】:

  • 在调用destroy之前,newSphere.AddComponent&lt;cubeControls&gt;()
  • @SanSolo 哇哦!这比我想象的要简单得多,你知道我在哪里可以读到更多吗?我想确保我理解它为什么起作用:)
  • 搜索如何在运行时向游戏对象添加脚本。本质上,脚本也是一个组件。所以添加/删除就像添加/删除任何组件一样

标签: c# visual-studio unity3d transform gameobject


【解决方案1】:

通常使用 Unity,我们不会实例化原语并手动向其添加组件,这样做通常不是长期做事的最佳方式,因为非程序员很难更改功能。更糟糕的是,它需要更改代码才能改变任何工作方式! Unity的主要优点是它是一个功能齐全的大型可视化编辑器,我们应该尽可能多地使用它,否则使用Unity还有什么意义呢?

一般来说,您想要做的是为每个实体(立方体和球体)创建一个预制件,并将脚本附加到两者。然后,无需实例化一个原始球体并尝试在脚本中从头开始构建它,您只需调用 GameObject.Instantiate() 来创建新球体。

public class cubeControls : MonoBehaviour
{
    // Constants for object rotation
    public GAmeObject spherePrefab;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.X))
        {
            Instantiate(spherePrefab, transform.position, transform.rotation);
        }
    }
}

这有几个好处,一,您可以完全控制编辑器内的球体,想将不同的组件附加到您的球体?您可以这样做而无需更改任何代码!想要稍微不同的参数?没问题。艺术家是否想为球体添加酷炫的轨迹效果?他们可以在没有程序员帮助的情况下做到这一点。

如果您需要修改球体上的变量(例如传输立方体的剩余生命值?),您可以在新实例化的球体预制件上调用 GetComponent(),并根据需要调用函数/编辑变量。

【讨论】:

  • 感谢您提供的信息!这真的很有帮助;我实际上是一名艺术家,所以这对我来说是全新的。 :) 再次感谢!
猜你喜欢
  • 2019-11-25
  • 1970-01-01
  • 2011-07-28
  • 1970-01-01
  • 1970-01-01
  • 2023-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多