我是Unity的新手,正在制作一款自上而下的游戏。 我编写了一个脚本,该脚本根据播放器是在对象的前面还是后面来更改对象在图层中的顺序,但是我无法弄清楚如何立即更改所有对象的图层。 (我试图让他们成为一个对象的所有子对象,但它不起作用) 我当前的代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Perspective_changer : MonoBehaviour {
void Update () {
GameObject librarian = GameObject.Find("Librarian");
Layer_orderer librarian_layer = librarian.GetComponent<Layer_orderer>();
librarian_layer.y_pos = transform.position.y;
GameObject blue_book_stand = GameObject.Find("Blue");
Layer_orderer blue_book_stand_layer = blue_book_stand.GetComponent<Layer_orderer>();
blue_book_stand_layer.y_pos = transform.position.y;
GameObject purple_book_stand = GameObject.Find("Purple");
Layer_orderer purple_book_stand_layer = purple_book_stand.GetComponent<Layer_orderer>();
purple_book_stand_layer.y_pos = transform.position.y;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Layer_orderer : MonoBehaviour {
public float y_pos;
void Start() {
}
void Update() {
if (transform.position.y > y_pos)
{
GetComponent<SpriteRenderer>().sortingOrder = 1;
}
else
{
GetComponent<SpriteRenderer>().sortingOrder = 3;
}
}
}
答案 0 :(得分:2)
您可以使用FindObjectsOfType并循环执行
public class Perspective_changer : MonoBehaviour
{
private void Update ()
{
var layerOrderers= FindObjectsOfType<Layer_orderer>();librarian = GameObject.Find("Librarian");
foreach(var layer in layerOrderers)
{
layer.y_pos = transform.position.y;
}
}
}
注意:FindObjectsOfType
仅会在场景中找到活动和启用的组件。
但是,尽可能不要在Find
中使用GetComponent
和Update
或任何重复方法,因为它们非常昂贵。最好在Awake
或Start
中执行一次,然后重用引用。 (我使用规则Awake
获取组件并设置自己的值-Start
用于获取其他组件/ GameObjects的组件/值)
public class Perspective_changer : MonoBehaviour
{
Layer_orderer[] layerOrderers;
private void Start()
{
layerOrderers = FindObjectsOfType<Layer_orderer>();
}
private void Update ()
{
foreach(var layer in layerOrderers)
{
layer.y_pos = transform.position.y;
}
}
}
Layer_orderer
public class Layer_orderer : MonoBehaviour
{
public float y_pos;
private SpriteRenderer _spriteRenderer;
private void Awake()
{
_spriteRenderer = GetComponent<SpriteRenderer>();
}
private void Update()
{
// for single values this is easier to read/write
_spriteRenderer.sortingOrder = transform.position.y > y_pos ? 1 : 3;
}
}
答案 1 :(得分:0)