0

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

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

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]);
   }
}
4

1 回答 1

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);
        }
    }
}
于 2019-10-14T18:52:24.087 回答