我正在尝试为我的2d小行星观察者创建一个健康系统。我现在使用健康图标实例化:
Transform newHealthIcon = ((GameObject)Instantiate(healthIconGUI.gameObject)).transform;
然而,当我尝试设置它的变换位置和旋转时,纹理变得不可见并且不再显示。
Transform newHealthIcon = ((GameObject)Instantiate(healthIconGUI.gameObject, transform.position, Quaternion.identity)).transform;
这是完整的代码:
using UnityEngine;
using System.Collections;
public class Health : MonoBehaviour {
public int startingHealth;
public int healthPerIcon;
public GUITexture healthIconGUI;
private ArrayList healthIcons = new ArrayList();
public int maxIconsPerRow;
private float spacingX;
private float spacingY;
void Start () {
spacingX = healthIconGUI.pixelInset.width;
spacingY = healthIconGUI.pixelInset.height;
AddHealth (startingHealth / healthPerIcon);
}
public void AddHealth(int n) {
for (int i = 0; i < n; i++) {
Transform newHealthIcon = ((GameObject)Instantiate(healthIconGUI.gameObject, transform.position, Quaternion.identity)).transform;
newHealthIcon.parent = transform;
int y = Mathf.FloorToInt(healthIcons.Count / maxIconsPerRow);
int x = healthIcons.Count - y * maxIconsPerRow;
newHealthIcon.GetComponent<GUITexture> ().pixelInset = new Rect(x * spacingX, y * spacingY, 1, 1);
healthIcons.Add (newHealthIcon);
}
}
public void ModifyHealth (int amount) {
}
}