c# - How to set an initial value for UI_Knob?[Solved] -
i trying set initial value ui.extensions ui_knob see link ui.extensions repo ui.extensions bitbutcket
through searching have found maybe best way set initial value use simulated pointereventdata , pass script using executeevents, since ui_knob script uses mouse position set it's value.
--below code ui_knob--
/// credit tomasz schelenz /// sourced - https://bitbucket.org/ddreaper/unity-ui-
//only allow rotation pointer on control public void onpointerdown(pointereventdata eventdata) { _candrag = true; } public void onpointerup(pointereventdata eventdata) { _candrag = false; } public void onpointerenter(pointereventdata eventdata) { _candrag = true; } public void onpointerexit(pointereventdata eventdata) { _candrag = true; // can drag pointer off control. } public void onbegindrag(pointereventdata eventdata) { setinitpointerdata(eventdata); } void setinitpointerdata(pointereventdata eventdata) { _initrotation = transform.rotation; _currentvector = eventdata.position - (vector2)transform.position; _initangle = mathf.atan2(_currentvector.y, _currentvector.x) * mathf.rad2deg; } public void ondrag(pointereventdata eventdata) { //check if can drag if (!_candrag) { setinitpointerdata(eventdata); return; } _currentvector = eventdata.position - (vector2)transform.position; _currentangle = mathf.atan2(_currentvector.y, _currentvector.x) * mathf.rad2deg; quaternion addrotation = quaternion.angleaxis(_currentangle - _initangle, this.transform.forward); addrotation.eulerangles = new vector3(0, 0, addrotation.eulerangles.z); quaternion finalrotation = _initrotation * addrotation; if (direction == direction.cw) { knobvalue = 1 - (finalrotation.eulerangles.z / 360f); if (snaptoposition) { snaptoposition(ref knobvalue); finalrotation.eulerangles = new vector3(0, 0, 360 - 360 * knobvalue); } } else { knobvalue = (finalrotation.eulerangles.z / 360f); if (snaptoposition) { snaptoposition(ref knobvalue); finalrotation.eulerangles = new vector3(0, 0, 360 * knobvalue); } } //prevent overrotation if (mathf.abs(knobvalue - _previousvalue) > 0.5f) { if (knobvalue < 0.5f && loops > 1 && _currentloops < loops - 1) { _currentloops++; } else if (knobvalue > 0.5f && _currentloops >= 1) { _currentloops--; } else { if (knobvalue > 0.5f && _currentloops == 0) { knobvalue = 0; transform.localeulerangles = vector3.zero; setinitpointerdata(eventdata); invokeevents(knobvalue + _currentloops); return; } else if (knobvalue < 0.5f && _currentloops == loops - 1) { knobvalue = 1; transform.localeulerangles = vector3.zero; setinitpointerdata(eventdata); invokeevents(knobvalue + _currentloops); return; } } } //check max value if (maxvalue > 0) { if (knobvalue + _currentloops > maxvalue) { knobvalue = maxvalue; float maxangle = direction == direction.cw ? 360f - 360f * maxvalue : 360f * maxvalue; transform.localeulerangles = new vector3(0, 0, maxangle); setinitpointerdata(eventdata); invokeevents(knobvalue); return; } } transform.rotation = finalrotation; invokeevents(knobvalue + _currentloops); _previousvalue = knobvalue; } private void snaptoposition(ref float knobvalue) { float snapstep = 1 / (float)snapstepsperloop; float newvalue = mathf.round(knobvalue / snapstep) * snapstep; knobvalue = newvalue; } private void invokeevents(float value) { if (clampoutput01) value /= loops; onvaluechanged.invoke(value); } } [system.serializable] public class knobfloatvalueevent : unityevent<float> { }
}
i answered own question without using simulated mouse data. instead, added start() method mystartingangle() method. see below. chopped ui_knob's method rotation/setting value , injected own angle. still know how perform simulated mouse data if willing. appreciate input on solution well. reading!!
// add ui_knob script below initialization. // added here allow setting 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; }
Comments
Post a Comment