结合两个实例化脚本(Unity 3D)

时间:2019-10-14 17:31:50

标签: arrays unity3d instantiation destroy virtual-tour

我需要将以下脚本组合成一个包含两个功能的脚本,一个脚本实例化阵列中的下一个预制件,另一个实例化阵列中的前一个预制件(这是一个虚拟导游应用程序)。两者都应该破坏当前的预制件。然后,我可以通过事件触发器调用这些函数。

我有用于“下一个预制”脚本的代码。

public GameObject[] Spheres;
    int currentIndex = 0;
    GameObject currentObject;
    public Camera MainCamera;

    void OnMouseDown()
    {
        if (Input.GetMouseButtonDown(0))
            if (gameObject.tag == "ArrowNEXT")
        {
            Vector3 rayOrigin = MainCamera.ViewportToWorldPoint(new Vector3(0.1f, 0.1f, 0));
            RaycastHit hit;

            if (Physics.Raycast(rayOrigin, MainCamera.transform.forward, out hit))
            {
                if (hit.collider != null)
                {
                    {
                            Destroy(currentObject);
                            currentIndex++;
                            if (currentIndex > Spheres.Length - 1) currentIndex = 0;
                            currentObject = Instantiate(Spheres[currentIndex]);
                    }
                }
            }


        }

    }

我需要结合以下内容:

using UnityEngine;


public class RayCastPrevFIX: MonoBehaviour
{
    public GameObject[] Spheres;
    int currentIndex = 0;
    GameObject currentObject;
    public Camera MainCamera;

    void OnMouseDown()
    {
        if (Input.GetMouseButtonDown(0))
            if (gameObject.tag == "ArrowPREV")
            {
            Vector3 rayOrigin = MainCamera.ViewportToWorldPoint(new Vector3(0.1f, 0.1f, 0));
            RaycastHit hit;

            if (Physics.Raycast(rayOrigin, MainCamera.transform.forward, out hit))
            {
                if (hit.collider != null)
                {
                    {
                            Destroy(currentObject);
                            currentIndex--;
                            if (currentIndex < 0) currentIndex = Spheres.Length - 1;
                            currentObject = Instantiate(Spheres[currentIndex]);
                    }
                }
            }


        }

    }
}

我将如何处理?非常感谢您的帮助。到目前为止,我已经做到了:

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

public class SphereSwap : MonoBehaviour
{ 
   public GameObject[] Spheres;
   int currentIndex = 0;
   GameObject currentObject;
   public Camera MainCamera;

   void Next()
   {
       Destroy(currentObject);
       currentIndex++;
       if (currentIndex > Spheres.Length - 1) currentIndex = 0;
       currentObject = Instantiate(Spheres[currentIndex]);
   }

   void Previous()
   {
       Destroy(currentObject);
       currentIndex--;
       if (currentIndex < 0) currentIndex = Spheres.Length - 1;
       currentObject = Instantiate(Spheres[currentIndex]);
   }
}

1 个答案:

答案 0 :(得分:0)

您走在正确的道路上,但是您可以缩短代码并在两种情况下都使用一个函数来避免样板:

using UnityEngine;
public class SphereSwap : MonoBehaviour
{
    public GameObject[] Spheres;
    int currentIndex = 0;
    GameObject currentObject;
    public Camera MainCamera;

    const string ArrowPrevTag = "ArrowPREV";
    const string ArrowNextTag = "ArrowNEXT";


    private void HandleClick(bool next)
    {
        if(Spheres == null || Spheres.Length == 0)
        {
            Debug.Log($"Spheres list is empty, nothing to swap.");
            return;
        }

        Vector3 rayOrigin = MainCamera.ViewportToWorldPoint(new Vector3(0.1f, 0.1f, 0));
        RaycastHit hit;
        if (Physics.Raycast(rayOrigin, MainCamera.transform.forward, out hit))
        {
            if (hit.collider != null)
            {
                // destroy current sphere.
                Destroy(currentObject);
                // go next or previous.
                currentIndex += next ? 1 : -1;

                // circular clamp if overflow
                if (currentIndex < 0)
                    currentIndex = Spheres.Length - 1;
                else if (currentIndex >= Spheres.Length)
                    currentIndex = 0;
                // finally do instantiate.
                currentObject = Instantiate(Spheres[currentIndex]);
            }
        }

    }
    private void OnMouseDown()
    {
        if (Input.GetMouseButtonDown(0))
        {
            // 'CompareTag' is more efficient than gameobject.tag == "sometag"
            if (gameObject.CompareTag(ArrowNextTag))
                HandleClick(true);
            else if (gameObject.CompareTag(ArrowPrevTag))
                HandleClick(false);
        }
    }
}