ImageBG腳本
usingUnityEngine;
usingSystem.Collections;
usingUnityEngine.UI;
//不手動添加ScrollRect組件机蔗,讓腳本繼承與ScrollRect邑茄,從而獲得ScrollRect的屬性帆啃、方法
public class ScrollCricle:ScrollRect{
private float? mRadius;
private Vector3?offsetVector3=Vector3.zero;//搖桿的偏移
protected override void? Start( ){
base.Start( );
//計算搖桿移動半徑
mRadius=(transform? as RectTransform).sizeDelta.x*0.5f;
}
voidUpdate( ){
CalculateOffect();
}
public override void OnDrag(UnityEngine.EventSystems.PointerEventData?eventData){
//獲得父類屬性
base.OnDrag(eventData);
base.content.anchoredPosition=Vector3.ClampMagnitude(base.content.anchoredPosition,mRadius);
}
//計算偏移
privatevoidCalculateOffect( ){
//(-1,1)~(1秀鞭,1)
offsetVector3=content.localPosition/mRadius;
}
public Vector3?GetoffsetVector3( ){
return offsetVector3;
}
}
被控制物體腳本
usingUnityEngine;
usingSystem.Collections;
usingSystem.Collections.Generic;
public class CubeMove:MonoBehaviour{
public ScrollCricle?scrollCricle;//獲得Scroll腳本
voidStart( ){
}
voidUpdate( ){
Vector3?offset=scrollCricle.GetoffsetVector3();
//物體的朝向隨著搖桿的方向移動(四元數(shù))
transform.rotation=Quaternion.LookRotation(new Vector3(offset.x,0,offset.y));
transform.position+=newVector3(offset.x,0,offset.y)*10*Time.deltaTime;
}
}