【问题标题】:unity 3d make prefab object undestroyable/unclibableunity 3d 使预制对象不可破坏/不可释放
【发布时间】:2017-02-01 17:06:10
【问题描述】:

好的,我制作了代表匹配的预制件。将脚本附加到主摄像头后,我进行了 15 次匹配,

using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;

public class LoadMatches : MonoBehaviour {
public GameObject match;

GameObject[] matchArray = new GameObject[15];

void Start () {

    DrawMatches();
 }
private void DrawMatches()
{


    int y = 4;
    int x = -4;
    int n = 0;
          for (int i = -4; i < 5; i += 2)
        {
            for (int j = x; j < y + 1; j += 2)
            {
             matchArray[n]=Instantiate(match, new Vector3(j, i, 0), Quaternion.identity) as GameObject;
             matchArray[n].name= Convert.ToString(n);
             n++;
            }
            y = y - 1;
            x = x + 1;
        }
}

}

我得到了这个

enter image description here

当我单击对象时,对象必须被销毁(制作),但是当我单击某行中的对象时,我必须使只有该对象可以被销毁,其他行中的所有其他匹配器都必须变得不可销毁/ unclickabe until button ispressed.

using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;
public class DragDrop : MonoBehaviour {
float x;
float y;
public GameObject match;
private txtGameOver txtGO;

private void Awake()
{
    txtGO = GameObject.FindObjectOfType<txtGameOver>();
}

private void Start()
{
    match.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeAll;

 }
 void Update(){
    x = Input.mousePosition.x;
    y = Input.mousePosition.y;
}
void OnMouseDrag(){
    transform.position = Camera.main.ScreenToWorldPoint(new Vector3(x,y,10.0f));
}
void OnMouseDown()
{
    var boxes = GameObject.FindGameObjectsWithTag("Match");

    SelectMatches(match);
    if (boxes.Length == 1)
    {
        txtGO.GameOverText();
    }
}
void SelectMatches (GameObject m)
{
   int n;
   n = Convert.ToInt32(m.name);
     if (n>=0 && n<=4)
    {

        Destroy(m);
     }
  else if (n > 4 && n <= 8)
    {
         Destroy(m);
     }
    else if (n > 8 && n <= 11)
    {
        Destroy(m);
    }
    else if (n > 11 && n <= 13)
    {
         Destroy(m);
    }
    else if (n==14)
    {
        Destroy(m);
     }
}

}

我的问题是如何在脚本中使某些对象不可破坏/不可点击。

【问题讨论】:

    标签: unity3d destroy


    【解决方案1】:

    通过这样做。您只需将游戏对象标记为 DontDestroy 而且是不可破坏的

        object[] obj = GameObject.FindObjectsOfType(typeof (GameObject));
        foreach (object o in obj) {
            GameObject go = (GameObject) o;
    
            //if the GO is tagged with DontDestroy, ignore it. (Cameras, Managers, etc. which should survive loading)
            //these kind of GO's shouldn't have an ObjectIdentifier component!
            if(go.CompareTag("DontDestroy")) {
                Debug.Log("Keeping GameObject in the scene: " + go.name);
                continue;
    
            }
            Destroy(go);
        }
    

    希望能帮到你

    【讨论】:

    • 有趣的方法。我会尝试那个解决方案。
    • 如果此答案有用@IstarEnki,请单击向上箭头。谢谢
    • 我放了箭头,但我的声誉低于 15,并且我收到了 clikc 将记录的 msg。但是Thnks,我明白了。现在我正在尝试在我的脚本中采用这个解决方案。我遇到了一些问题,因为我使用了 0 到 14 不同名称的 15 个对象,现在我正在寻找如何接近它们
    • 接近他们是什么意思。让我帮你解释清楚:)
    • 相信我是菜鸟的错误,我解决了一个问题,并且很容易与我的代码合并。这是答案,非常感谢。我接受了这个喜欢的答案。
    【解决方案2】:

    好的,我做了很简单的解决方案。

    using UnityEngine;
    using System.Collections;
    using System;
    using UnityEngine.UI;
    using System.Collections.Generic;
    
    public class DragDrop : MonoBehaviour {
    float x;
    float y;
    public GameObject match;
    private txtGameOver txtGO;
     public GameObject[] GameObjectArray;
    
    private void Awake()
    {
        txtGO = GameObject.FindObjectOfType<txtGameOver>();
    }
    
    private void Start()
    {
      GameObjectArray = GameObject.FindGameObjectsWithTag("Match");
    }
     void Update(){
        x = Input.mousePosition.x;
        y = Input.mousePosition.y;
    }
    void OnMouseDrag(){
     transform.position = Camera.main.ScreenToWorldPoint(new Vector3(x,y,10.0f));
    }
    void OnMouseDown()
    {
        var boxes = GameObject.FindGameObjectsWithTag("Match");
    
        SelectMatches(match);
        if (boxes.Length == 1)
        {
            txtGO.GameOverText();
        }
    }
    void SelectMatches (GameObject m)
    {
       int n;
       n = Convert.ToInt32(m.name);
       if (n>=0 && n<=4)
        {
            if (GameObjectArray[n].CompareTag("Match") && GlobaVariables.Control==false)
            {
                ChangeTag(0, 4,n);
    
            }
            else if (GameObjectArray[n].CompareTag("Destroy") && GlobaVariables.Control == true)
            {
                GameObjectArray[n].SetActive(false);
            }
    
        }
      else if (n > 4 && n <= 8)
        {
            if (GameObjectArray[n].CompareTag("Match") && GlobaVariables.Control == false)
            {
                ChangeTag(5, 8, n);
    
            }
            else if (GameObjectArray[n].CompareTag("Destroy") && GlobaVariables.Control == true)
            {
                GameObjectArray[n].SetActive(false);
            }
        }
        else if (n > 8 && n <= 11)
        {
            if (GameObjectArray[n].CompareTag("Match") && GlobaVariables.Control == false)
            {
                ChangeTag(9, 11,n);
    
            }
            else if (GameObjectArray[n].CompareTag("Destroy") && GlobaVariables.Control == true)
            {
                GameObjectArray[n].SetActive(false);
            }
        }
        else if (n > 11 && n <= 13)
        {
            if (GameObjectArray[n].CompareTag("Match") && GlobaVariables.Control == false)
            {
                ChangeTag(12, 13, n);
    
            }
            else if (GameObjectArray[n].CompareTag("Destroy") && GlobaVariables.Control == true)
            {
                GameObjectArray[n].SetActive(false);
            }
        }
        else if (n==14)
        {
            if (GameObjectArray[n].CompareTag("Match") && GlobaVariables.Control == false)
            {
                ChangeTag(14, 14, n);
    
            }
            else if (GameObjectArray[n].CompareTag("Destroy") && GlobaVariables.Control == true)
            {
                GameObjectArray[n].SetActive(false);
            }
        }
     }
     void ChangeTag(int p, int z,int n)
     {
    
        for (int i = p; i <z+1; i++)
        {
    
          GameObjectArray[i].tag = "Destroy";
            Debug.Log(GameObjectArray[i].tag + " " + GameObjectArray[i]);
    
    
        }
    
        GameObjectArray[n].SetActive(false);
        GlobaVariables.Control = true;
    
    }
    }
    

    在函数选择匹配项中,我检查了一个 GameObject 数组,其中是 Matces/Playing 对象,并检查了对象的数量/名称。我知道对象的位置,并且我只更改名称范围内的对象。我制作了全局静态布尔变量,它是中断块更改标签,直到我按下按钮。因为数组我不使用destroy对象,所以SetActvie tru更好,因为这种方法不会弄乱数组。

    【讨论】:

      【解决方案3】:

      您可以将 UI 设置为在对象的特定位置不接受输入,基本上说光标不在对象上方,因此您无法单击。这是我能找到/想到的最接近的帮助。

      public class GraphicIgnoreRaycast : MonoBehaviour, ICanvasRaycastFilter
       {
           public bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
           {
               return false;
           }
       }
      

      有关 ^ 该概念/脚本的更多信息:http://answers.unity3d.com/questions/816861/46-ui-image-is-capturing-clicks-how-to-prevent.html

      至于设置一个对象不能被销毁,Unity就不是这样的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多