I have a question regarding my code:
I have a list of player "private List player = new List();" and Player is a class that I mad myself, I also created an Usercontrol and all variable of player are databinded.
And it's working perfectly fine in my main form, but I have a strange issue when I change form.
I give the player list as a parameter to another form and here is the code:
public Result(List<Player> player, string format)
{
InitializeComponent();
_player = player;
ExtentionHelpers.Shuffle<Player>(_player);
}
Shuffle as the name said, just shuffle the list, but it should shuffle _player which is a local variable of my second form, I never use player in this form.
But for some strange reason, my list player from my main form end up being shuffled too, and I don't want that, specially when that mess up my databinding.
What's happening here ??
Your player variable is a reference type, so it is the same data you are working on in both forms. You'll need to clone your player list if you want to manipulate it separately.
When calling your constructor, try this:
var frm = new Result(new List<Player>(player), "some format");
Creating a new list means that you can manipulate the new list independently from the first BUT if you change data within the Player type, it will affect data from the other form (you'll need to clone each item separately before adding to a new list if you want this).
Related
I am creating a map in Unity3D to be displayed in Hololens 2. I am using MRTK to code interactable functionality.
Since the map contains a lot of independent countries, whose respond onClick may vary with time, I decided to add the required components programmatically. For example:
public Transform worlmap;
public Microsoft.MixedReality.Toolkit.UI.Theme mTheme;
public Microsoft.MixedReality.Toolkit.UI.States mStates;
List<Microsoft.MixedReality.Toolkit.UI.Theme> themes;
void Start()
{
themes = new List<Microsoft.MixedReality.Toolkit.UI.Theme>();
if (mTheme)
{
themes.Add(mTheme);
}
// Looping through all the countries in worldMap
foreach (Transform country in worldMap)
{
// Adding the 4 components which make a GameObject interactable in Hololens
country.gameObject.AddComponent<Microsoft.MixedReality.Toolkit.UI.Interactable>();
country.gameObject.AddComponent<Microsoft.MixedReality.Toolkit.UI.PressableButton>();
country.gameObject.AddComponent<Microsoft.MixedReality.Toolkit.Input.NearInteractionTouchable>();
country.gameObject.AddComponent<MeshCollider>();
// Assigning State
country.gameObject.GetComponent<Microsoft.MixedReality.Toolkit.UI.Interactable>().States = mStates;
// Assigning the Profiles (Will definine the colors based on the State)
Microsoft.MixedReality.Toolkit.UI.InteractableProfileItem mProfile = new Microsoft.MixedReality.Toolkit.UI.InteractableProfileItem();
mProfile.Themes = themes;
mProfile.Target = country.gameObject;
country.gameObject.GetComponent<Microsoft.MixedReality.Toolkit.UI.Interactable>().Profiles.Add(mProfile);
// Assigning Events
// TODO
}
}
The part below is working, but I am not able to assign Events to the different countries in the same loop.
I can define these events in the Editor, for example:
But since it is a repetitive activity which may slightly change over time, I was trying to achieve the same programmatically inside the loop of the script above.
This is what I tried, but it is not working:
// Creating an Event
Microsoft.MixedReality.Toolkit.UI.InteractableEvent mEvent = new Microsoft.MixedReality.Toolkit.UI.InteractableEvent();
// Assigning the method to be executed during onClick
mEvent.Event.AddListener(country.gameObject.GetComponent<CountrySelected>().printName)
// Assigning the Event to Interactable
country.gameObject.GetComponent<Microsoft.MixedReality.Toolkit.UI.Interactable>().InteractableEvents.Add(mEvent);
What is the correct way of adding programmatically Events to an Interactable in MRTK?
To assign OnClick Event for the Interactable component at the runtime, please try the following code:
this.GetComponent<Interactable>().OnClick.AddListener(MyFun1);
The Interactable component profile in the Unity Editor may not display the new method at the runtime, but it does work in the scene.
I have a Class Dogs, and one of its properties is called MyPictureBox (type of PictureBox). I also have four picture boxes on the windows form.
I have this piece of code assigning corresponding pictureBoxes to the instance of each Dog. And this works just fine.
Dogs[0].MyPictureBox = pictureBoxDog1;
Dogs[1].MyPictureBox = pictureBoxDog2;
Dogs[2].MyPictureBox = pictureBoxDog3;
Dogs[3].MyPictureBox = pictureBoxDog4;
I spent some time trying to cycle through Dogs and assign pictureBox using its name (as the only difference in picture box name is the ending 1,2,3,4) with no luck.
Any ideas are greatly appreciated (hate to move ahead without solving this first).
From what I can get from your question, I think your dog should have a method like showOnPictureBox(PictureBox p) and maybe a property that holds the PictureBox that the dog is currently "in".
I need to implement input fields in unity which will be added on the fly as per the elements in a list. It should increase as per the objects present in a map "m_myClassMap".
Also, is it possible to fetch the position, layout settings ( i.e. x,y,z coordinates) of the existing Inputfields? I want these info to calculate the position of new inputfield.
For e.g.
List<InputField> inputFieldList = new List<InputField>();
foreach (MyClass myClassObj in m_myClassMap.Values)
{
// code below is just statement and is syntactically wrong
InputField newInputField = new InputField();
// Add properties like size or layout
// set the text
inputFieldList.Text = myClassObj.getInputBoxString();
inputFieldList.Add(newInputField );
}
I am not sure if its even possible..Any help will be welcome :)
Thanks!
The most robust thing to do here would be to use a layout group. This method plenty of well tested functionality.
Firstly add a a parent object. And add a Vertical Layout Group component to it. I've shown the hierarchy below by adding a Red panel to the blue panel. On the red panel is the Vertical Layout Group.
In this example I've used the inspector to manually add 4 input fields. Note the layout here is all automatic. I've included the settings on this particular input field below, you'll be able to experiment in the inspector and very quickly see what impact the various options have.
Now for your particular solution, you want to add the items programatically rather than through the inspector. For this you'll want to create a GameObject with a reference to the object holding the layout group.
Programatic Creation
The below code will add a list of prefabs to a class. (In this case the parent class should contain the Vertical Layout Group component)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LayoutGroupExample : MonoBehaviour {
public GameObject layoutObject;
public GameObject inputFieldPrefab;
void AddInputFields(){
var inputFieldList = new List<string>(){"First Name", "Last Name", "Email"};
foreach(var fieldName in inputFieldList){
GameObject go = GameObject.Instantiate(inputFieldPrefab);
go.name = fieldName;
go.transform.SetParent(layoutObject.transform);
var inputField = go.AddComponent<InputField>();
}
}
}
In the inspector assign the object containing the layout group to the layoutObject field.
Assign a prefab (which should be an Input Field object) to the inputFieldPrefab field)
I'm almost done doing a Connect4 game with VS2012 using WinForms. Everything is working well but I wanted to bring the options for the user on a dedicated Start Menu window. On that menu I have two comboBoxes I need to take the text from to use them as a value for two variables in my other form (the game window). I also have one New Game button that should call a method from my other form if that's possible (basically, I made an "Initialization()" method in my game form and I'd like it to be launched when I click the "New Game" button on the other form).
I only found tutorials that show how to do very basic things from one form to an other (such as labeling texts) but I I didn't find an answer to my specific problem.
I used this in my main form to instantiate the menu form
public FormMenu myMenu;
myMenu = new FormMenu();
What I want to do is that I could do something like this in the other form :
amountOfRows = Int32.Parse(myMenu.comboBoxRows.Text);
amountOfColumns = Int32.Parse(myMenu.comboBoxColumns.Text);
Any idea how I could do that?
I would love to see some example code so I can help see where your confusion lies. WinForms requires the other form to be instantiated.
OtherForm form = new OtherForm();
Once the form is instantiated you should be able to run code from it.
EDIT:
Based on your implementation I would suggest making public methods within FormMenu that return these int values.
public int ReturnRows()
{
return Int32.Parse(myMenu.comboBoxRows.Text);
}
public int ReturnColumns()
{
return Int32.Parse(myMenu.comboBoxColumns.Text);
}
Then from the other form in which myMenu is instantiated you can call myMenu.ReturnRows() and myMenu.ReturnColumns()
The easiest way would be to store a reference to your form in the menue as a variable. (you already named it myMenu)
Then you should create the property/properties you need in the form an add a setter for the values. (see example here)
Last you update the form fields with
myMenu.property = newvalue;
That`s all about it
I created a game in c# for windows 8 and everything works like it is supposed to but after the user plays the game the score is displayed in a text box and it is supposed to also display on the following screen displaying the score and text and thumbs up images depending on how high it is.
The problem is the score isn't being carried over. The code I use gets the constructor from the game page then the score, the code is below:
Var arcade = new arcadeMode();
ScoreNum.text = arcade.score.toString();
//then sample code to display images based on score
if(arcade.score < 100)
{
Usermessage.text = "try again.";
Thumbdown.visibility = visibility.visible;
}
I think my problem is actually getting the saved score from the game because it must only be getting the textbox value from before the game started. Not really sure how to save the score without a database though. Any suggestions on how to go about this?
It looks like ArcadeMode is essentially your data model, but gets the constructor from the game page doesn't really have meaning. Assuming you have an instance of ArcadeMode on your game page and it includes the actual score, with the code above, you've just created a brand new second instance of that same class with the score initialized to 0.
One quick way, since it appears it's just a simple piece of data you want to pass, is use the second parameter of Navigate. Wherever it is you navigate to the thumbs-up page, add the current value of your 'score' variable as the second parameter, for example:
Frame.Navigate(typeof(ThumbsUpPage), score)
Then in the OnNavigatedTo event of ThumbsUpPage you can access the value you passed via the Parameter property of the argument, for example:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
String score = e.Parameter;
...
Another option is to give the ArcadeMode class instance a larger scope (i.e., not make it a variable on the main page). You could make it a member of the App class, for instance, and then it would be available for all the pages in your app.