我不能设置相机选项。当我玩游戏并加载另一个场景时,相机选项会发生变化。但是,当我在同一场景中点击剧本时,它的工作原理是正确的!控制台说:
MissingComponentException:在“画布”游戏对象上没有“照相机”,但是脚本正在尝试访问它。 您可能需要添加一个相机到游戏对象“画布”。或者您的脚本需要在使用之前检查组件是否已附加。“
下面是我当前的脚本:
using UnityEngine;
using System.Collections;
public class PixelPerfectCamera : MonoBehaviour {
public static float PixelsToUnits = 1f;
public static float scale = 1f;
public Vector2 nativeResolution = new Vector2(400, 160);
void Awake()
{
var camera = GetComponent<Camera>();
if (camera.orthographic)
{
scale = Screen.height / nativeResolution.y;
PixelsToUnits *= scale;
camera.orthographicSize = (Screen.height / 2.0f) / PixelsToUnits;
}
}
}发布于 2016-04-15 19:02:46
您不有一个相机附加到-相同的GameObject,PixelPerfectCamera脚本是附加的。
如果你的场景中只有一台相机,那就换一台吧。
var camera = GetComponent<Camera>(); 至
Camera camera = Camera.main; //Use the main camera如果场景中有多个摄像机,请使用:
Camera camera = GameObject.Find("NameOfGameObjectCameraIsAttachedTo").GetComponent<Camera>(); //Use the main camera您必须将 NameOfGameObjectCameraIsAttachedTo更改为摄像机附加的GameObject的名称。
发布于 2016-04-16 08:17:57
将脚本更改为:
using UnityEngine;
using System.Collections;
public class PixelPerfectCamera : MonoBehaviour {
public static float PixelsToUnits = 1f;
public static float scale = 1f;
public Vector2 nativeResolution = new Vector2(400, 160);
public Camera camera;
void Awake()
{
if (camera.orthographic)
{
scale = Screen.height / nativeResolution.y;
PixelsToUnits *= scale;
camera.orthographicSize = (Screen.height / 2.0f) / PixelsToUnits;
}
}
}然后将场景中的相机附加到这个脚本的摄像机字段。
发布于 2016-04-18 11:20:44
您可能想看看RequireComponent,它不是必需的,但是它在这些问题上有很大的帮助。http://docs.unity3d.com/ScriptReference/RequireComponent.html
https://stackoverflow.com/questions/36654798
复制相似问题