I have a question.
Here's My inspector Window.
In case of On Click() window, I'd like to set parameter that is type of Enum.
not string or int.
In other words, I'd like to use
void GoToNext(DATA_TYPE type).
But that doesn't show up.
Even if I set my enum as [SerializedField], that doesn't show in this window.
How can I do this?
I found a great solution to this from the user 'TobiLaForge' on this Unity forum. At least it is the best solution worked for me where I had only a few enums to handle.
1) Declare your enum outside of your class you use it in or create it anywhere else outside of a MonoBehaviour.
2) Create a script, attach it to your button:
using UnityEngine;
public class GetEnum : MonoBehaviour{
public MyEnum state;
}
3 Add this or change your orginial function where you use the enum
public void GetEnumState(GetEnum g)
{ if(g.state == MyEnum.something)
DoSomething();
}
4) In the OnClick() function slot select your function and drag the GetEnum script into the slot.
This will need a new MonoBehaviour script for each enum you use in that way. Here is my inspector after.
You can't currently do this, Unity doesn't support it. But, since enums are basically ints, perhaps you could setup your function to accept an int and somehow cast it to the appropriate enum?
I tried this little experiment in some code I have with an enum, and it seemed to work fine:
public enum unitType {orc_warrior, archer, none};
int test2 = 0;
unitType test;
test = (unitType)test2;
Debug.Log(test);
test2 = 1;
test = (unitType)test2;
Debug.Log(test);
Debug correctly printed out orc_warrior and then archer
To make clear how it works what nipunasudha suggest, here the full code example how I use it. Please notice the need of ´SerializeReference´ instad of ´SerializeField´ otherwise all your buttons would end with the same state - which is usually not what you want to achieve.
using UnityEngine;
public class MenuInvoker : MonoBehaviour
{
private enum State { Main, Settings };
[SerializeReference] private State state;
public void InvokeMenu(MenuInvoker currState)
{
switch (currState.state)
{
case State.Main:
Debug.Log("Main");
break;
case State.Settings:
Debug.Log("Settings");
break;
default:
Debug.Log("Default Menu");
break;
}
}
}
Related
I know there are several topics already on stackoverflow, but nothing that actually solves the problem. Here it is:
Because of some inherent problems with Ribbon Designer I decided to build my next Excel AddIn using XML Ribbon.
However, occasionally I need to make changes to the controls in the ribbon based on user selections. For example I need to change the text of a label, and also make some of the controls disabled in some cases. And here's where I hit a brick wall. It looks like there's no way to do it. I tried to put the logic in the onAction callback as follows:
public void LabelAction(IRibbonControl control)
{
LabelControl label = (LabelControl)control;
label.Label = "changed text";
}
But this cast doesn't work because apparently IRibbonControl interface has nothing to do with the RibbonControl class that LabelConrol inherits from.
I was also not able to find any other way to access any of the XML ribbon controls. Is there even a solution to this? Or should I stick to Ribbon Designer?
You need to do this in a routine that sets the item label.
The xml would look like this:
<button id="SkLabelTest1" getLabel="GetLabelTest" onAction="SkLabelTest1"/>
<button id="SkLabelTest2" getLabel="GetLabelTest" onAction="SkLabelTest2"/>
The routine you are interested in is getLabel
I've done a noddy routine to demonstrate this.
First I added a property to ThisAddin.cs for it to read:
public string _labelTest = string.Empty;
public string LabelTest { get { return _labelTest; } set { _labelTest = value; } }
Then in my ribbon handling code I added the getLabel routine:
public string GetLabelTest(Office.IRibbonControl control)
{
switch (control.Id.ToLower())
{
case "sklabeltest2":
if (Globals.ThisAddIn.LabelTest != string.Empty)
return Globals.ThisAddIn.LabelTest;
else
return "Label Test 2";
default:
return "Label Test 1";
}
}
This works by the SkLabelTest1 button changing the text of SkLabelTest2 and then invalidating the control to force the ribbon to reload it:
public void SkLabelTest1(Office.IRibbonControl control)
{
Globals.ThisAddIn._labelTest = "Changed text";
Globals.ThisAddIn._ribbon.InvalidateControl("SkLabelTest2");
}
I've tested just in case and it changes the text OK. Hope this helps
I couldn't make a comment because of my reputation. As a comment to Charlie's post, it is a perfect solution but on my side, I had to change one part.
I changed public void SklabelTest1 function to this one below:
public void SkLabelTest1(Office.IRibbonControl control)
{
Globals.ThisAddIn._labelTest = "Changed text";
this.ribbon.InvalidateControl("SkLabelTest2");
}
And also added this in the beginning of my ribbon class.
private Office.IRibbonUI ribbon;
I hope it helps.
i have a question, how can i use this variables in standart class?
Sample
ref class test
{
Button ^x;
};
works good, but i need not managed class..
and this not work
class test
{
Button ^x;
};
maybe anybody have a solution?
You could accomplish this with the help of gcroot like this:
class test
{
gcroot<Button^> *x;
};
You can then create this field's object in your constructor like:
x = gcnew Button();
and generally use x like a Button^...
Im trying to set the Range of a slider within a subclass, catching the respective controller using getController, which works fine, proven by the returned value i get within the print. But controller.setRange() doesnt get recognized as a function.
can the range only be initialized during the creation of the object or does getController return a different object than i expect it does?
thanks!
class Stepper
{
String stepperName = "default";
int ID = motorID;
int stepperValue;
Stepper(String givenName) {
stepperName = givenName;
cp5.addSlider(stepperName)
.setPosition(sliderPosX+(sliderWidth+100)*surgeonBotID, sliderPosY+90*servoList.length+30*(motorID-servoList.length))
.setSize(sliderWidth, int(sliderHeight*0.5))
//.setRange(0, 179)
.setSliderMode(Slider.FLEXIBLE);
println("Created new Stepper: "+stepperName+ " ID: "+ID);
motorID++;
}
void setRange(float min, float max){
println("object: "+cp5.getController(getStepperName()).getValue());
cp5.getController(getStepperName()).setRange(min, max);
}
...
}
Questions like these are best answered by consulting the API.
The getController() method returns a Controller. The Controller class does not have a setRange() function. That instance happens to be an instance of Slider, which is a subclass of Controller, but the compiler has no way of knowing that. That's what's causing your error.
You can tell the compiler that the instance is indeed a Slider by casting the returned value to a Slider, and then you can access the methods defined by the Slider class:
((Slider)cp5.getController(getStepperName())).setRange(min, max);
To make that easier to understand, here it is split up into two lines:
Slider s = (Slider)cp5.getController(getStepperName());
s.setRange(min, max);
I see a lot of legacy .Net 1.1-style code at work like in example below, which I would like to shrink with the help of an auto-property. This will help many classes shrink by 30-40%, which I think would be good.
public int MyIntThingy
{
get
{
return _myIntThingy;
}
set
{
_myIntThingy = value;
}
} private int _myIntThingy = -1;
This would become:
public int MyIntThingy
{
get;
set;
}
And the only question is - where do I set MyIntThingy = -1;?
If I wrote the class from the start, then I would have a better idea, but I did not. An obvious answer would be: put it in the constructor. Trouble is: there are many constructors in this class. Watching the initialization to -1 in the debugger, I see it happen (I believe) before the constructor gets called. It is almost as if I need to use a static constructor as described here:
http://www.c-sharpcorner.com/uploadfile/cupadhyay/staticconstructors11092005061428am/staticconstructors.aspx
except that my variables are not static. Java's static initializer comes to mind, but again - my variables are not static. http://www.glenmccl.com/tip_003.htm
I want to make stylistic but not functional changes to this class. As crappy as it is, it has been tested and working for a few years now. breaking the functionality would be bad. So ... I am looking for shorter, sweeter, cuter, and yet EQUIVALENT code. Let me know if you have questions.
I'm afraid that you have no option.
If you want to use an auto-property with an initial value that differs from the type's default value then you'll need to set the initial value in the constructor(s).
If you just need a stylistic, non-breaking change, consider changing the format a little:
public int MyIntThingy
{
get { return _myIntThingy; }
set { _myIntThingy = value; }
}
private int _myIntThingy = -1;
Isn't that prettier?
And consider using auto-properties for future code only. It sounds too risky to use them on existing code, unless there are no default values.
I am using a basiceditfield to take input from the user to do some simple string search. But if i type a few letters and wish to go back without continuing the search, it automatically asks me whether to save the contents of the field. I don want this to happen. Can i in any way disable the "Changes made!-save-discard-cancel" option in basiceditfield(or any editfield for that matter)????please help!!!
Try adding this to your MainScreen class:
protected boolean onSavePrompt() {
return true;
}
Another way would be to override the dirty state logic on your Screen class like this:
public boolean isDirty() { return false; }
Of course you can also just override that same method on a subclass of your Field, and that too should probably work. (assuming you still want to do dirty-state-tracking of other fields on the screen.)
modify onClose method of Screen
public boolean onClose() {
this.close();
return true;
}