统一3d使预制对象不可破坏/不可克服

时间:2017-02-01 17:06:10

标签: unity3d destroy

好的,我做了preafab whic代表比赛。随着脚本附加到主相机我做了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

当我点击对象对象时必须被销毁(做成)但当我点击某一行中的对象时,我必须做出只有那个对象可以被销毁所有其他行中的匹配器必须变得不可破坏/ unlickabe直到按下按钮。

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

}

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

3 个答案:

答案 0 :(得分:2)

通过这样做。你只需将游戏对象标记为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);
    }

希望我帮助

答案 1 :(得分:1)

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

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;

}
}

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

答案 2 :(得分:0)

您可以将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所做的事情。