C# 如何设置UI_旋钮的初始值?

C# 如何设置UI_旋钮的初始值?,c#,user-interface,unity3d,C#,User Interface,Unity3d,我正在尝试为UI.Extensions UI\u旋钮设置初始值 请参阅指向UI.Extensions repo的链接 通过一些搜索,我发现也许设置初始值的最佳方法是使用模拟pointerEventData并使用ExecuteEvents将其传递给脚本,因为UI_旋钮脚本使用鼠标位置来设置其值 --下面是UI_旋钮的代码-- ///托马斯谢伦茨信用卡 ///来源于—— //仅允许指针在控件上旋转 POINTERDOWN上的公共无效(PointerEventData事件数据) { _坎德拉=真; }

我正在尝试为UI.Extensions UI\u旋钮设置初始值 请参阅指向UI.Extensions repo的链接

通过一些搜索,我发现也许设置初始值的最佳方法是使用模拟pointerEventData并使用ExecuteEvents将其传递给脚本,因为UI_旋钮脚本使用鼠标位置来设置其值

--下面是UI_旋钮的代码--

///托马斯谢伦茨信用卡 ///来源于——

//仅允许指针在控件上旋转
POINTERDOWN上的公共无效(PointerEventData事件数据)
{
_坎德拉=真;
}
POINTERUP上的公共无效(PointerEventData事件数据)
{
_坎德拉=假;
}
公共void OnPointerEnter(pointereventdataeventdata)
{
_坎德拉=真;
}
公共void OnPointerExit(pointereventdataeventdata)
{
_canDrag=true;//现在可以在指针不受控制的情况下拖动。
}
公共void OnBeginDrag(PointerEventData事件数据)
{
SetInitPointerData(事件数据);
}
void SetInitPointerData(PointerEventData事件数据)
{
_initRotation=transform.rotation;
_currentVector=eventData.position-(Vector2)transform.position;
_initAngle=Mathf.Atan2(_currentVector.y,_currentVector.x)*Mathf.Rad2Deg;
}
public void OnDrag(PointerEventData事件数据)
{
//检查是否可以拖动
如果(!\u坎德拉)
{
SetInitPointerData(事件数据);
返回;
}
_currentVector=eventData.position-(Vector2)transform.position;
_currentAngle=Mathf.Atan2(_currentVector.y,_currentVector.x)*Mathf.Rad2Deg;
四元数addRotation=四元数.AngleAxis(_currentAngle-_initAngle,this.transform.forward);
addRotation.eulerAngles=新矢量3(0,0,addRotation.eulerAngles.z);
四元数最终旋转=_initRotation*addRotation;
如果(方向==方向.CW)
{
knobValue=1-(最终旋转.eulerAngles.z/360f);
如果(捕捉位置)
{
SnapToPosition(参考knobValue);
finalRotation.eulerAngles=新矢量3(0,0,360-360*knobValue);
}
}
其他的
{
knobValue=(finalRotation.eulerAngles.z/360f);
如果(捕捉位置)
{
SnapToPosition(参考knobValue);
finalRotation.eulerAngles=新矢量3(0,0,360*knobValue);
}
}
//防止过度旋转
如果(数学绝对值(knobValue-_previousValue)>0.5f)
{
如果(knobValue<0.5f&&loops>1&&U currentLoops0.5f&&U currentLoops>=1)
{
_电流环--;
}
其他的
{
如果(knobValue>0.5f&&u currentLoops==0)
{
knobValue=0;
transform.localEulerAngles=Vector3.0;
SetInitPointerData(事件数据);
调用事件(knobValue+_currentLoops);
返回;
}
否则如果(knobValue<0.5f&&u currentLoops==loops-1)
{
knobValue=1;
transform.localEulerAngles=Vector3.0;
SetInitPointerData(事件数据);
调用事件(knobValue+_currentLoops);
返回;
}
}
}
//检查最大值
如果(最大值>0)
{
if(knobValue+_currentLoops>maxValue)
{
knobValue=最大值;
浮点最大角度=方向==方向.CW?360f-360f*maxValue:360f*maxValue;
transform.localEulerAngles=新矢量3(0,0,maxAngle);
SetInitPointerData(事件数据);
调用事件(knobValue);
返回;
}
}
transform.rotation=最终旋转;
调用事件(knobValue+_currentLoops);
_previousValue=knobValue;
}
专用空心捕捉位置(参考浮动旋钮值)
{
float snapStep=1/(float)snapStepsPerLoop;
float newValue=Mathf.Round(knobValue/snapStep)*snapStep;
knobValue=newValue;
}
私有void InvokeEvents(浮点值)
{
如果(卡箍输出01)
值/=循环;
OnValueChanged.Invoke(值);
}
}
[系统可序列化]
公共类KnobFloatValueEvent:UnityEvent{}

}我回答了自己的问题,没有使用模拟鼠标数据。相反,我用MyStartingAngle()方法添加了一个Start()方法。见下文。我切碎了UI_旋钮的旋转/设置值方法,基本上只注入了我自己的角度。如果有人愿意,我仍然想知道如何使用模拟鼠标数据执行此操作。如果您能为我的解决方案提供任何意见,我将不胜感激。谢谢你的阅读

// ADD THIS INTO UI_Knob script below initialization.
// I added this here to allow for setting an initial rotation/value.

    void Start(){
        float myFirstAngle = 180f;
        MyStartingAngle (myFirstAngle);
    }

    void MyStartingAngle(float angle){

        _initRotation = transform.rotation;

        Quaternion addRotation = Quaternion.AngleAxis(angle, this.transform.forward);
        addRotation.eulerAngles = new Vector3(0, 0, addRotation.eulerAngles.z);

        Quaternion finalRotation = _initRotation * addRotation;


        knobValue = 1 - (finalRotation.eulerAngles.z / 360f);

        transform.rotation = finalRotation;
        InvokeEvents(knobValue + _currentLoops);

        _previousValue = knobValue;

    }

对不起,我不明白这个问题。你能澄清一下吗?
// ADD THIS INTO UI_Knob script below initialization.
// I added this here to allow for setting an initial rotation/value.

    void Start(){
        float myFirstAngle = 180f;
        MyStartingAngle (myFirstAngle);
    }

    void MyStartingAngle(float angle){

        _initRotation = transform.rotation;

        Quaternion addRotation = Quaternion.AngleAxis(angle, this.transform.forward);
        addRotation.eulerAngles = new Vector3(0, 0, addRotation.eulerAngles.z);

        Quaternion finalRotation = _initRotation * addRotation;


        knobValue = 1 - (finalRotation.eulerAngles.z / 360f);

        transform.rotation = finalRotation;
        InvokeEvents(knobValue + _currentLoops);

        _previousValue = knobValue;

    }