I have the following GUI:
As can be seen - this dialog has a title and two buttons.
The dialog title is label that i control using a property in a Dialog script, and the buttons are represented as OptionsButton.
What I want to do is to control these values from scripts, so if I set the title from the inspector - it'll be visible in editor mode and in play mode as initial value. Same deal goes to the texture - so if I'll set the texture from the inspector (not with Unity built-in inspector field but in my custom field) - it'll update both editor and play mode.
Lets take a better example with the title: for the title, I have the following property:
[TabGroup("Game Objects")]
[Required]
public GameObject dialogTitleObject;
/// <summary>
/// Gets or sets the dialog title.
/// </summary>
/// <value>The dialog title.</value>
public string Title
{
get { return this.dialogTitleObject.GetComponent<UILabel>().text; }
set { this.dialogTitleObject.GetComponent<UILabel>().text = value; }
}
So, at runtime I do able to modify this value successfully. However - I want to have the following inspector:
And it should be able to modify the value in the Editor Mode (not in Play Mode) the value will be modified accordingly.
Right now I can't do such thing and even if i type in the inspector textbox - nothing will happen.
Thanks for any help!
So, after few days of reading and tweaking, I came to the solution I posted in this public gist: https://gist.github.com/YahavGB/dea6289a9bd6e6fba82885abbd8da820 .
Basically, I've used a getter and setter methods instead of actual C# properties. Then I created a drawer that sync between the field and the methods.
Finally, to make sure i dont add extra fields, as I just needed to "forward" the call, and not to actually add another variable, I added a macro check for UNITY_EDITOR. Thus I do add another field ( :( ) but just in the editor and not in the actual production game.
I hope this solution will aid someone in the future.
So basic you need build a Custom Inspector
https://unity3d.com/learn/tutorials/topics/interface-essentials/building-custom-inspector
This will bind unity to variable.
Related
I want a button on my winform to be enabled or disabled based on the return value of a particular function. Basically, I'm trying the following code in various places where the function will possible return a different value:
btnNewNotices.Enabled = isSelectedPrinterValid();
this.btnNewNotices.Refresh()
However, this is not working. Why is it when I call the refresh method after the enabled property is changed, that the button does not become enabled? I have to close the form and reopen it before the button properly disables. What is the best way to accomplish what I need here without having to bounce the form?
you can add a Databinding for the Enabled property.
if your method is implemented in your Form then you can define a Property
public bool IsSelectedPrinterValid
{
get{ return this.isSelectedPrinterValid(); }
}
And add a Databinding as following:
btnNewNotices.DataBindings.Add("Enabled", this, "IsSelectedPrinterValid");
You can refresh your value:
btnNewNotices.DataBindings[0].ReadValue();
The common way is to implement a ViewModel containing all Properties and Methods you need and bind your controls to these.
Short answer, but it's pretty straight forward.
Using .Refresh(); causes the button to repaint itself, and resets the Enabled property. There's no reason to use it in your context. Just remove it.
btnNewNotices.Enabled = isSelectedPrinterValid();
I want to force the user to select values from a popped up window if no value is provided for the relevant field when trying to save (for instance saving a contact) in xaf web application? How can this be achieved?
With the XAF framework you can achieve most things, but it depends how much you want to fight with it.
The XAF way is to use the validation module and to add a RuleRequiredField to your property (documentation). Then when a user presses save an error message will appear and the relevant field will be focused. After the user has fixed the offending field, they can click save again. Not exactly what you describe but it works very well. Something like this:
private string _Title;
[RuleRequiredField("RuleID_TitleIsRequired", DefaultContexts.Save, "A title must be specified.")]
public string Title {
get { return _Title; }
set { SetPropertyValue("Title", ref _Title, value); }
}
Any reference field in edit mode can appear as a popup window. There are various alternatives for reference property editors all documented here and in your case you can control which type of editor appears (ASPxLookupFindEdit which is a popup window or ASPxLookupDropDownEdit which is a drop down) via the model.
If you are set on a popup appearing after pressing Save, then you would have to override the behaviour in the WebModificationsController. There is some documentation here. It would be a feasible change, but if you are not experienced with XAF I would recommend sticking with the validation rule.
I am trying to make my game have the ability to right mouse button click on web builds.
void MouseCheck()
{
if(Input.GetMouseButton(1))
{
//my code
}
}
But it doesn't get detected because when I right click in web build it shows some default options such as full screen.
This is a bit tricky, but not impossible.
You need to make changes on the built's HTML. This is how you would do it:
var params =
{
disableContextMenu: true,
};
This parameter will notify Unity Web Player whether it should display the ContextMenu or not. This in turn prevents the context menu from appearing, which then allows your game to check for Right-Mouse Clicks.
Here is a link to all the customization you can do to the HTML file: Unity WebPlayer's Behaviours.
It is important that you include the C# code which does the actual right click checks happen.
if(Input.GetMouseButtonDown(1))
{
// Code here.
}
This was added for future readers who might benefit from this part
Let's say I create a custom control which embed a trackbar. I also create an orientation property for my custom control.
When I drop the custom control on a form by default it will be horizontal. Then I set it to vertical, the trackbar should refresh to be vertical at design time.
How to do so ?
I think you should call Refresh() after changing the value:
public OrientationProperty Direction
{
get
{
return _direction;
}
set
{
_direction = value;
if (DesignMode)
{
Parent.Refresh(); // Refreshes the client area of the parent control
}
}
}
private OrientationProperty _direction;
Here's my solution to this issue:
1. Whenever you set something property, call Invalidate() in the setter.
2. After correspondent properties and refreshing method (for eg. overridden OnPaint) are implemented, rebuild!!! then you'll see the modifications taken effect in design time
3. During design, always check whether compilation errors are present, as this might stop VS performing all his tasks.
With this, when I put my control on a form, and adjust its own properties, refreshing happens immediately as expected.
PS.: old post, but at least verified the behavior in VS2015 too :)
I apologise if the title was confusing, it took me nearly 5 minutes to finally think of a title for this one...
Okay, you know how in Visual Studio Express when you add a TabControl to the Form, and you can click on the right-arrow on the top right of the TabControl and it will add a new TabPage, or remove one?
Well, I'm creating a User Control where I need people to be able to switch between Panels (my user control is made up of several Panels). I know this is possible as I've used a Ribbon Control in the past and you could add new buttons etc in the Designer View.
Can somebody please provide any suggestions/advice on how I might go about acheiving this?
Thank you
If I understand your question correctly, you're talking about smart tags.
The process is a little bit involved, so I'm not going to try to post a complete sample. Instead, I'll refer you to this tutorial on the subject. To make a long story short, you have to create a custom designer, and register one or more custom actions. You can use this to create a combo box listing the available panels and switch between them when the selected item is changed.
(Note - the term "smart tags" has two distinct meanings in Visual Studio - I'm specifically talking about the visual designer smart tags, not smart tags in the code editor).
When you make a control that is inherited from Control, you have to make use of a couple of properties such as IsDesignMode, you can then construct event handlers especially for within Design Mode:
if (IsDesignMode){
// Handle the interactivity in Design mode, such as changing a property on the
// Properties toolbox
}
Suppose the control has an event such as MouseClick, you can do this:
private void control_MouseClick(object sender, MouseEventArgs e){
if (IsDesignMode){
// Do something here depending on the Click event within the Designer
}else{
// This is at run-time...
}
}
Another I can think of is 'ShouldSerialize' followed by a publicly accessible property in order to persist the property to the designer-generated code, suppose for example a Control has a boolean property Foo
public bool Foo{
get{ return this._foo; }
set{ if (this._foo != value){
this._foo = value;
}
}
}
public bool ShouldSerializeFoo(){
return true; // The property will be persisted in the designer-generated code
// Check in Form.Designer.cs...
}
If ShouldSerializeFoo returned false, no property is persisted, its the opposite when true, it will be buried within the Form.Designer.cs code...
Hope this helps,
Best regards,
Tom.