Unity3d unity 3d使预制对象不可删除/不可删除

Unity3d unity 3d使预制对象不可删除/不可删除,unity3d,destroy,Unity3d,Destroy,好的,我做了预赛,预赛代表比赛。我把脚本连在主摄像机上,做了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 () {

好的,我做了预赛,预赛代表比赛。我把脚本连在主摄像机上,做了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;
        }
}
使用UnityEngine;
使用系统集合;
使用制度;
使用UnityEngine.UI;
公共类LoadMatches:单行为{
公开游戏对象匹配;
GameObject[]matchArray=新游戏对象[15];
无效开始(){
DrawMatches();
}
私有void DrawMatches()
{
int y=4;
int x=-4;
int n=0;
对于(int i=-4;i<5;i+=2)
{
对于(int j=x;j
}

我得到了这个

当我点击该对象时,该对象必须被销毁(制作),但当我点击某一行中的对象时,我必须制作只有该对象才能被销毁,其他行中的所有匹配器必须变得不可删除/不可读取,直到按下按钮

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);
     }
}
使用UnityEngine;
使用系统集合;
使用制度;
使用UnityEngine.UI;
公共类DragDrop:单行为{
浮动x;
浮动y;
公开游戏对象匹配;
私人txtGameOver txtGO;
私人空间
{
txtGO=GameObject.FindObjectOfType();
}
私有void Start()
{
match.GetComponent().constraints=RigidbodyConstraints2D.FreezeAll;
}
无效更新(){
x=输入.mousePosition.x;
y=输入.mousePosition.y;
}
void OnMouseDrag(){
transform.position=Camera.main.ScreenToWorldPoint(新矢量3(x,y,10.0f));
}
void OnMouseDown()
{
var-box=GameObject.FindGameObjectsWithTag(“匹配”);
选择matches(匹配);
如果(box.Length==1)
{
txtGO.GameOverText();
}
}
void SelectMatches(游戏对象m)
{
int n;
n=转换为32(m.name);

如果(n>=0&&n4&&n8&&n11&&n您可以将UI设置为不在对象的某个位置接收输入,基本上说光标不在对象上,因此您无法单击。这是我能找到/想到的最有助于此的方法

public class GraphicIgnoreRaycast : MonoBehaviour, ICanvasRaycastFilter
 {
     public bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
     {
         return false;
     }
 }
有关该概念/脚本的详细信息:


至于将一个对象设置为无法销毁,这不是Unity所能做到的。

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

希望我帮了你

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

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;

}
}
使用UnityEngine;
使用系统集合;
使用制度;
使用UnityEngine.UI;
使用System.Collections.Generic;
公共类DragDrop:单行为{
浮动x;
浮动y;
公开游戏对象匹配;
私人txtGameOver txtGO;
公共GameObject[]GameObjectArray;
私人空间
{
txtGO=GameObject.FindObjectOfType();
}
私有void Start()
{
GameObjectArray=GameObject.FindGameObjectsWithTag(“匹配”);
}
无效更新(){
x=输入.mousePosition.x;
y=输入.mousePosition.y;
}
void OnMouseDrag(){
transform.position=Camera.main.ScreenToWorldPoint(新矢量3(x,y,10.0f));
}
void OnMouseDown()
{
var-box=GameObject.FindGameObjectsWithTag(“匹配”);
选择matches(匹配);
如果(box.Length==1)
{
txtGO.GameOverText();
}
}
void SelectMatches(游戏对象m)
{
int n;
n=转换为32(m.name);

如果(n>=0&&n 4&&n 8&&n 11&&n有趣的方法。我将尝试该解决方案。如果此答案有用,请单击向上箭头@IstarEnki。谢谢我放置箭头,但我的声誉不到15,我收到了clikc将记录的消息。但是,我有了主意。现在我正在尝试将此解决方案应用到我的脚本中。我遇到了一些问题,因为我a 15个不同名称的对象0到14,现在我正在研究如何接近它们。你接近它们是什么意思。让我帮你清楚地解释一下:)相信我,没有错误,我解决了一个问题,很容易与我的代码合并。这是答案,非常感谢。我接受了这个类似的答案。