Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 统一:让操纵杆填满整个屏幕?_C#_User Interface_Unity3d_Joystick - Fatal编程技术网

C# 统一:让操纵杆填满整个屏幕?

C# 统一:让操纵杆填满整个屏幕?,c#,user-interface,unity3d,joystick,C#,User Interface,Unity3d,Joystick,好的,我从Unity asset store下载了一个标准的操纵杆,这里的脚本(不是很长): //实现IPointerDownHandler、IPointerUpHandler、IDragHandler以订阅指针事件 公共类操纵杆:单行为、IPInterDownHandler、IPInterUpHandler、IDragHandler{ 公共代表无效操纵(矢量2操纵); 公共静态事件JoystickAction JoystickMoved; 公共矢量2欢乐节; 使用操纵杆的公共图书馆; 私密图像

好的,我从Unity asset store下载了一个标准的操纵杆,这里的脚本(不是很长):

//实现IPointerDownHandler、IPointerUpHandler、IDragHandler以订阅指针事件
公共类操纵杆:单行为、IPInterDownHandler、IPInterUpHandler、IDragHandler{
公共代表无效操纵(矢量2操纵);
公共静态事件JoystickAction JoystickMoved;
公共矢量2欢乐节;
使用操纵杆的公共图书馆;
私密图像;
私人形象粘贴;
私有矩形变换;
私有化;转移;粘着转移;
//第一次轻敲的处理方式类似于任何后续的阻力检测
POINTERDOWN上的公共虚拟无效(PointerEventData ped){
使用操纵杆=真;
昂德拉格(ped);
}
POINTERUP上的公共虚拟无效(PointerEventData ped){
使用操纵杆=错误;
joystickAxes=stickImg.GetComponent().anchoredPosition=Vector2.0;
//joystickAxes=新矢量2(屏幕高度/2,屏幕宽度/2);
如果(JoystickMoved!=null)JoystickMoved(Vector2.zero);
}
公共虚拟void OnDrag(PointerEventData-ped){
向量2直肌;
如果(RectTransformUtility.ScreenPointToLocalPointInRectangle(
bgImg.rectTransform,
ped.position,
佩德:摄像机,
out rectPos){//检查指针是否位于操纵杆上
//将位置钳制在图像边界内,防止拖动到图像之外以获得更高的值。
Vector2 clampedPos=GetClampedPosition(rectPos);
//将操纵手柄轴规格化为介于-1和1之间。
joystickAxes=新矢量2(
钳位x/(bgTransform.rect.width/2f),
钳位y/(bgTransform.rect.height/2f));
//设置内部操纵手柄的位置。
如果(joystickAxes.magnity>1f){//规范化夹具位置会将操纵杆位置限制为一个圆
stickImg.GetComponent().anchoredPosition=clampedPos.normalized*stickTransform.rect.width;
}否则{
stickImg.GetComponent().anchoredPosition=clampedPos;
}
}
}
专用矢量2获取夹紧位置(矢量2位置){
Vector2 bgMin=bgTransform.rect.min;
Vector2 bgMax=bgTransform.rect.max;
返回新矢量2(
数学夹具(位置x,最小尺寸x,最大尺寸x),
数学夹具(位置y,最小值y,最大值y);
}
//用于初始化
无效开始(){
joystickAxes=新向量2();
bgImg=GetComponent();
stickImg=transform.GetChild(0.GetComponent();
bgTransform=gameObject.GetComponent();
stickTransform=transform.GetChild(0.GetComponent();
}
无效更新(){
如果(使用操纵杆和操纵杆移动)=null{
JoystickMoved(joystickAxes);
}
}
}
这依赖于画布上的图像对象和旋钮的另一个图像。如图所示:

这一切都很好,但我希望操纵杆是屏幕,这意味着旋钮的外部边界不是背景图像,而是屏幕边界本身

我试着用rh canvas等将背景图像缩放到屏幕上,但这会使输入值偏离目标。我该怎么做


我想保留这个脚本,只需调整它,使背景“图像”//边界成为屏幕,无论大小。

脚本本身将内部操纵杆移动限制为一个圆圈。在不改变脚本的情况下,我怀疑你是否可以将它的移动限制为一个矩形(即屏幕的形状),好吧-那么我如何才能将它更改为一个矩形?我会修改剧本的
// Implement IPointerDownHandler, IPointerUpHandler, IDragHandler to subscribe to pointer events
public class Joystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler {
    public delegate void JoystickAction(Vector2 joystickAxes);
    public static event JoystickAction JoystickMoved;

    public Vector2 joystickAxes;

    public bool usingJoystick;
    private Image bgImg;
    private Image stickImg;
    private RectTransform bgTransform;
    private RectTransform stickTransform;

    // The first tap is treated similar to any following detection of a drag
    public virtual void OnPointerDown(PointerEventData ped) {
        usingJoystick = true;
        OnDrag (ped);
    }

    public virtual void OnPointerUp(PointerEventData ped) {
        usingJoystick = false;
        joystickAxes = stickImg.GetComponent<RectTransform> ().anchoredPosition = Vector2.zero;
        //joystickAxes = new Vector2(Screen.height / 2, Screen.width/2);

        if (JoystickMoved != null) JoystickMoved (Vector2.zero);
    }

    public virtual void OnDrag(PointerEventData ped) {
        Vector2 rectPos;

        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(
                bgImg.rectTransform,
                ped.position,
                ped.enterEventCamera,
                out rectPos)) { // Check if the pointer is positioned on the joystick
            // Clamp the position to be inside the image bounds, prevents dragging beyond the image to get higher values.
            Vector2 clampedPos = GetClampedPosition (rectPos); 

            // Normalize the joystick axes to be between -1 and 1.
            joystickAxes = new Vector2 (
                clampedPos.x / (bgTransform.rect.width / 2f),
                clampedPos.y / (bgTransform.rect.height / 2f));

            // Set the position of the inner joystick.
            if (joystickAxes.magnitude > 1f) { // Normalizing the clampedPos constrains the joystick positions to a circle
                stickImg.GetComponent<RectTransform> ().anchoredPosition = clampedPos.normalized * stickTransform.rect.width;
            } else {
                stickImg.GetComponent<RectTransform> ().anchoredPosition = clampedPos;
            }
        }
    }

    private Vector2 GetClampedPosition(Vector2 pos) {
        Vector2 bgMin = bgTransform.rect.min;
        Vector2 bgMax = bgTransform.rect.max;

        return new Vector2 (
            Mathf.Clamp (pos.x, bgMin.x, bgMax.x),
            Mathf.Clamp (pos.y, bgMin.y, bgMax.y));
    }

    // Use this for initialization
    void Start () {
        joystickAxes = new Vector2 ();

        bgImg = GetComponent<Image> ();
        stickImg = transform.GetChild (0).GetComponent<Image> ();

        bgTransform = gameObject.GetComponent<RectTransform> ();
        stickTransform = transform.GetChild (0).GetComponent<RectTransform> ();
    }

    void Update() {
        if (usingJoystick && JoystickMoved != null) {
            JoystickMoved (joystickAxes);
        }
    }
}