Not able to pass two function parameters in MRTK - unity3d

I am trying to create a simple MRTK application which takes inputs from Manipulator Event( Game Object and Text) and displays Gameobject with updated text on the screen.
Example: When Manipulation is started, enable the game object and update the Text. For this I want the Manipulation event to pass the game object name and string to function which enables the object and update text.
I can see the function name with one parameter.
public void showpotext(GameObject txtframe)
{
//txtpo4 = GameObject.Find("poinfotxt4");
txtpo1.GetComponent<TextMeshProUGUI>().text = stxtpo1;
txtpo2.GetComponent<TextMeshProUGUI>().text = stxtpo2;
txtpo3.GetComponent<TextMeshProUGUI>().text = stxtpo3;
txtpo4.GetComponent<TextMeshProUGUI>().text = stxtpo4;
txtframe.SetActive(true);
}
It works for the above code, I can see the function name in the Manipulator event.
But If I add one more parameter to my function, I am not able to see the Function name.
public void showpotext(GameObject txtframe, string updatedtext)
{
//txtpo4 = GameObject.Find("poinfotxt4");
//txtpo1.GetComponent<TextMeshProUGUI>().text = stxtpo1;
//txtpo2.GetComponent<TextMeshProUGUI>().text = stxtpo2;
//txtpo3.GetComponent<TextMeshProUGUI>().text = stxtpo3;
txtpo4.GetComponent<TextMeshProUGUI>().text = updatedtext;
txtframe.SetActive(true);
}
}
Am I doing anything wrong?
Regards,

Related

Storing script names in a variable

new to unity so please ignore if I sound stupid.
I have to scripts references in a script. Example, script A and B are referenced in script C.
I need to store script A and B variable names in a another variable so that I can use that variable in conditioning.
private FemalePlayerAnimations femalePlayerAnimations;
private MalePlayerAnimations malePlayerAnimations;
private Variable variable; // Got Problem Here
void Awake()
{
femalePlayerAnimations = GetComponent<FemalePlayerAnimations>();
malePlayerAnimations = GetComponent<MalePlayerAnimations>();
}
void Start()
{
if(1 + 1 = 2) // Some Condition
{
variable = femalePlayerAnimations;
}
else if(1 + 2 = 3) // Some Another Condition
{
variable = malePlayerAnimation;
}
}
Thanks in advance.
If I understand your question correctly, you'll need to use inheritence and have your male/female animations inherit from the same base class.
i.e.
public abstract class BasePlayerAnimator : MonoBehavior {}
public class MalePlayerAnimator : BasePlayerAnimator {}
public class FemalePlayerAnimator : BasePlayerAnimator {}
Real question though is why do you need two different classes for male/female animations? wouldn't a single class with 2 different instances cover your needs?
I have a feeling you don't simply want the name of the variable as a string.
The logic you are trying to implement here won't work. You can't have a variable that holds the FemalePlayerAnimations class hold a MalePlayerAnimations class.
You should reconsider the design of your program as you can have two different instances (prefabs) of the same theoratical PlayerAnimations class. This is how Animation Controllers work in Unity.
Alternatively you could use a boolean field to store states, for example: bool useFemaleAnimations that is changed in the conditions and implement the correct "script" where applicable.

How do I create a Unity 3D UI text element in an F# script?

I'm attempting to create some ui elements in my F# script that is attached to a camera in Unity 3d. I know how to do this in C# but I'm not quite sure how to do it in F#.
Here's a simple C# example:
using UnityEngine;
using UnityEngine.UI;
public class CameraController : MonoBehaviour {
public Text uiText;
void Start ()
{
uiText.text = "Hello world!";
}
void Update ()
{
}
}
Now, here's my F# code. I've added comments that displays the compiler error I get when attempting to create a text element, This type has no accessible constructors.
namespace GameLogic
open UnityEngine
open UnityEngine.UI
type CameraController() =
inherit MonoBehaviour()
member this.uiText = Text // Compiler Error: This type has no accessible constructors
member this.Start() =
let uiText = Text // Compiler Error: This Type has no accessible constructors
()
member this.Update() =
()
It seems like F# implicitly calls a constructor method on Unity's Text object.
So, how can I create a ui text element in an F# script? Thanks for any and all help.
Kurt
In your C# code, the uiText member is getting initialized with null, which is the default for C#. In F#, there is no "default", you have to write the initial value explicitly:
member this.uiText: Text = null
This will compile, but won't give you the desired result. This syntax will create a read-only property that always returns null. In order to create a read/write property with initial value, you have to use member val:
member val uiText: Text = null with get, set
The 'get, set' part in there serves for clarification that you want the property to be readable and writable. Without it (by default), the property will be read-only.
For a complete description of how properties work in F#, see MSDN.
I ended up using a mutable let binding that specifies it is of type Text and its initial value is null. I then added the SerializeField attribute in order to associate UI Text elements in the Unity editor with the script.
Below is a simple example.
namespace GameLogic
open UnityEngine
open UnityEngine.UI
type CameraController() =
inherit MonoBehaviour()
[<SerializeField>]
let mutable uiText : Text = null
member this.Start() =
uiText.text <- "hello world!"
()
member this.Update() =
()

NTriplesParser extract textual value from string

I am using dotnetrdf and trying to parse some triples with NTriplesParser. I have my own handler RobHandler in which I process each triple in turn.
public class RobHandler : BaseRdfHandler
{
protected override bool HandleTripleInternal(Triple t)
{
string predicateUrl = ((BaseUriNode)(t.Predicate)).Uri.AbsoluteUri;
string value = t.Object.ToString();
}
}
This works fine but I want to get the object minus the language. My objects look like "Lincoln"#en. I could obviously write some code to remove the #en bit, but I'd rather use some library code rather than my own that hard-coded strings like #en. To do this I think I need to create a LiteralNode but there doesn't seem to be a way to get from a string which is what I have (my variable value) to a LiteralNode.
How can I extract just the textual value from an object string?
Actually I think I have the answer myself:
if (t.Object.NodeType == NodeType.Literal)
{
var node = (ILiteralNode)t.Object;
}

ReSharper 8 - Live Template Macros - HotspotItems

I am currently using ReSharper V8.1. I've only recently began using ReSharper and have found some interest in their LiveTemplate Macros. I've conjured up a solution to return a list of HotspotItems from a constant, similar to ReSharper's predefined macro "Comma-delimited list of values". In the method I take the constant variable of the template parameter and do a split string on them to provide a collection of HotSpotItems. Unfortunately it doesn't work if I use the macro more than one time within a template. Below is an extreme hack job showing my implementation of the method HotspotItems of IMacroImplementation.
I am hoping that someone out there may have done some work in this area and could possibly provide an example of how they've implemented IMacroImplementation which provides a list of items from a constant and also allows for multiple uses within a single template.
Thank you.
public override HotspotItems GetLookupItems(IHotspotContext context)
{
HotspotItems hotSpotItems = null;
foreach (var hotspot in context.HotspotSession.Hotspots)
{
if (hotspot.Expression != null && ((MacroCallExpressionNew)hotspot.Expression).Definition is Macros.DisplayMultipleItems)
{
//hotspot.CurrentValue
var multiItems = ((MacroCallExpressionNew) hotspot.Expression).Definition as DisplayMultipleItems;
if (!multiItems.ItemSet)
{
var expression = hotspot.Expression as MacroCallExpressionNew;
IMacroParameterValueNew baseValue = expression.Parameters[0].GetValue(context.SessionContext.Solution.GetLifetime(), context.HotspotSession);
string templateValue = baseValue.GetValue();
multiItems.ItemSet = true;
if (!string.IsNullOrEmpty(templateValue) && templateValue.Split(',').Any())
{
var lookupItems = templateValue.Split(',').Select(param => new TextLookupItem(param)).Cast<ILookupItem>().ToList();
if (hotSpotItems == null)
hotSpotItems = new HotspotItems(lookupItems);
else
{
foreach (var item in lookupItems)
{
hotSpotItems.Items.Add(item);
}
}
}
}
}
}
return hotSpotItems;
}
You should fire up dotPeek and point it to the ReSharper bin directory and take a look at ListMacroDef and ListMacroImpl, which is the implementation for the comma-delimited list macro.
The definition derives from SimpleMacroDefinition. It gets given the parameters in the call to GetPlaceholder, looks at the first and splits it by comma, returning the first item as the placeholder.
ListMacroImpl is just as simple. Its constructor has an [Optional] parameter of type MacroParameterValueCollection. This is the list of parameter values specified in the hotspot editor. You'll want to check for null and take the first parameter, which will be your delimited list. It then overrides GetLookupItems and returns HotspotItems.Empty if the parameter value is null, or parses the value and returns a list of TextLookupItem.
You don't need to look at the session and list of hotspots - that will get you all hotspots in the session, when you're only interested in the current hotspot, and ReSharper will create a new IMacroImplementation for each hotspot and give you those values in your constructor.

Unity3D. How to construct components programmatically

I'm new to unity and am having a bit of trouble getting my head around the architecture.
Lets say I have a C# script component called 'component A'.
I'd like component A to have an array of 100 other components of type 'component B'.
How do I construct the 'component B's programmatically with certain values?
Note that 'component B' is derived from 'MonoBehaviour' as it needs to be able to call 'StartCoroutine'
I'm aware that in unity you do not use constructors as you normally would in OO.
Note that 'component B' is derived from 'MonoBehaviour' as it needs to
be able to call 'StartCoroutine'
I'm aware that in unity you do not use constructors as you normally
would in OO.
That's true. A possibility is to istantiate components at runtime and provide a method to initialize them ( if initialization requires arguments, otherwise all initialization can be done inside Start or Awake methods ).
How do I construct the 'component B's programmatically with certain
values?
Here's a possible way:
public class BComponent : MonoBehavior
{
int id;
public void Init(int i)
{
id = i;
}
}
}
public class AComponent : MonoBehavior
{
private BComponent[] bs;
void Start()
{
bs = new BComponent[100];
for (int i=0; i < 100; ++i )
{
bs[i] = gameObject.AddComponent<BComponent>().Init(i);
}
}
}
Note that in the example above all components will be attached to the same GameObject, this might be not what you want. Eventually try to give more details.