unity 3d 制作预制对象 undestroyable/unclibable
unity 3d make prefab object undestroyable/unclibable
好的,我制作了代表火柴的预制件。将脚本附加到主摄像头后,我进行了 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
当我点击对象时,对象必须被销毁(使那个)但是当我点击某行中的对象时,我必须使只有那个对象可以被销毁,其他行中的所有其他匹配器必须成为undestroyable/unclickabe 直到按下按钮。
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);
}
}
}
我的问题是如何在脚本中制作一些对象 undestroyable/unclikable。
你可以设置UI在对象的特定位置不接受输入,基本上就是说光标不在对象上所以你不能点击。这是我能find/think 提供的最接近的帮助。
public class GraphicIgnoreRaycast : MonoBehaviour, ICanvasRaycastFilter
{
public bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
{
return false;
}
}
有关 concept/script 的更多信息:http://answers.unity3d.com/questions/816861/46-ui-image-is-capturing-clicks-how-to-prevent.html
至于将对象设置为无法被销毁,那是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;
}
}
在函数 select 匹配中,我检查了一个游戏对象数组,其中是 Matces/Playing 对象并检查了对象的 number/name。我知道对象的位置,并且我只更改名称范围内的对象。我制作了全局静态布尔变量,它是中断块更改标签,直到我按下按钮。因为数组我不使用销毁对象,SetActvie tru 更好,因为这种方法不会弄乱数组。
好的,我制作了代表火柴的预制件。将脚本附加到主摄像头后,我进行了 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
当我点击对象时,对象必须被销毁(使那个)但是当我点击某行中的对象时,我必须使只有那个对象可以被销毁,其他行中的所有其他匹配器必须成为undestroyable/unclickabe 直到按下按钮。
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);
}
}
}
我的问题是如何在脚本中制作一些对象 undestroyable/unclikable。
你可以设置UI在对象的特定位置不接受输入,基本上就是说光标不在对象上所以你不能点击。这是我能find/think 提供的最接近的帮助。
public class GraphicIgnoreRaycast : MonoBehaviour, ICanvasRaycastFilter
{
public bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
{
return false;
}
}
有关 concept/script 的更多信息:http://answers.unity3d.com/questions/816861/46-ui-image-is-capturing-clicks-how-to-prevent.html
至于将对象设置为无法被销毁,那是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;
}
}
在函数 select 匹配中,我检查了一个游戏对象数组,其中是 Matces/Playing 对象并检查了对象的 number/name。我知道对象的位置,并且我只更改名称范围内的对象。我制作了全局静态布尔变量,它是中断块更改标签,直到我按下按钮。因为数组我不使用销毁对象,SetActvie tru 更好,因为这种方法不会弄乱数组。