现在我的项目中有2个班级 我想在类1中使用方法类2 这段代码是c#
第1课
public class controlBuilding : MonoBehaviour
{
ScaleModel cScale = new ScaleModel();
public void Start()
{
}
public void Update()
{
cScale.touchScaleB1();
}
public void OnGUI()
{
}
}
第2课
public class ScaleModel : MonoBehaviour
{
public void touchScaleB1()
{
if (Input.touchCount >= 2)
{
Touch touch1 = Input.touches[0];
Touch touch2 = Input.touches[1];
if (touch1.phase == TouchPhase.Moved || touch2.phase == TouchPhase.Moved)
{
float pinchDistance = Vector2.Distance(touch1.position, touch2.position);
float prevDistance = Vector2.Distance(touch1.position - touch1.deltaPosition,
touch2.position - touch2.deltaPosition);
float pinchDistanceDelta = pinchDistance - prevDistance;
if (pinchDistanceDelta > 0)
{
float mScaleFactor;
mScaleFactor = b1_floor1dae.transform.localScale.x;
mScaleFactor += 0.0004f;
b1_floor1dae.transform.localScale = new Vector3(mScaleFactor, mScaleFactor, mScaleFactor);
b1_floor2dae.transform.localScale = new Vector3(mScaleFactor, mScaleFactor, mScaleFactor);
b1_floor3dae.transform.localScale = new Vector3(mScaleFactor, mScaleFactor, mScaleFactor);
b1_floor4dae.transform.localScale = new Vector3(mScaleFactor, mScaleFactor, mScaleFactor);
b1_floorTop.transform.localScale = new Vector3(mScaleFactor, mScaleFactor, mScaleFactor);
b1_other.transform.localScale = new Vector3(mScaleFactor, mScaleFactor, mScaleFactor);
}
else if (pinchDistanceDelta < 0)
{
float mScaleFactor;
mScaleFactor = b1_floor1dae.transform.localScale.x;
mScaleFactor -= 0.0004f;
b1_floor1dae.transform.localScale = new Vector3(mScaleFactor, mScaleFactor, mScaleFactor);
b1_floor2dae.transform.localScale = new Vector3(mScaleFactor, mScaleFactor, mScaleFactor);
b1_floor3dae.transform.localScale = new Vector3(mScaleFactor, mScaleFactor, mScaleFactor);
b1_floor4dae.transform.localScale = new Vector3(mScaleFactor, mScaleFactor, mScaleFactor);
b1_floorTop.transform.localScale = new Vector3(mScaleFactor, mScaleFactor, mScaleFactor);
b1_other.transform.localScale = new Vector3(mScaleFactor, mScaleFactor, mScaleFactor);
}
}
}
}
}
为什么我触摸屏幕不是事件?错误的代码在哪里?
对不起。我英语不好。
非常感谢您的回答。
答案 0 :(得分:2)
如果您希望ScaleModel只是一个实用程序类: 不要从MonoBehaviour继承它。因此,您可以使用“new”关键字作为常规类创建它。
如果您希望ScaleModel成为游戏对象组件: 将您的两个脚本添加到同一个游戏对象中并像这样更改您的第一个类... (通过调用GetComponent,您将在同一个游戏对象上搜索ScaleModel组件)
public class controlBuilding : MonoBehaviour
{
ScaleModel cScale;
public void Start()
{
cScale = GetComponent<ScaleModel>();
}
public void Update()
{
cScale.touchScaleB1();
}
public void OnGUI()
{
}
}