How to passing c# function as a param to UnityScript in unity? - c#

I have a class using UnityScript like this:
public class Responder{
private var completeHandler:Function;
public function addHandlers(completeHandler:Function):void {
completeHandler();
}
}
Now, i want to use this class in c# code,
public class MyGame : MonoBehaviour{
void Start(){
Responder res = new Responder();
res.addHandlers(?????); //how to pass the param??
}
}
Thanks!

Put the Javascript class in a folder called Plugins under assets. You will be able to call the methods written in JS from C# scripts.
The type Function in JS appears to me as Boo.Lang.ICallable in C#. As I couldn't import Boo.Lang by default, I took the Boo.Lang.dll from the Unity3D installed folder and copied it to the same Plugins folder in the project, then adding using Boo.Lang; at the top of my C# script.
You can now create and pass a new ICallable type as an argument from C# to JS.

Related

Unity: Access HLSL file like a TextAsset?

I would like to expose a public property in my script from which I can drag and drop an .hlsl file onto the script component in the inspector window, like in this image, which shows an exposed property to drop a TextAsset:
The code powering this is:
public class NativeSDKWrapper : MonoBehaviour
{
public TextAsset ShaderFile;
// ...
}
According to the Unity Manual page on TextAsset, .hlsl is not listed as a supported file type. How can I get around this?
I need to read in the contents of an HLSL file to a string, so that I can compile it with D3DCompile.
You can use the Editor class ScriptedImporter to handle this in your editor:
using UnityEngine;
using UnityEditor.AssetImporters;
using System.IO;
[ScriptedImporter(1, "hlsl")]
public class CubeImporter : ScriptedImporter
{
public override void OnImportAsset(AssetImportContext ctx)
{
var ta = new TextAsset(File.ReadAllText(ctx.assetPath));
ctx.AddObjectToAsset("main obj", ta);
ctx.SetMainObject(ta);
}
}
Code based on documentation code linked above.
Be sure to put it in your project's Editor folder, as it references Editor stuff that won't be accessible once built.

How to make function "invisible" in a class library c#

I am creating a class library and have different functions inside. I also have a Console application that can access this functions once they reference the class library. I would like to know how to make a function "invisible" so the client won't be able to see it exist and they can only use it if they write it out perfectly.
I have this function in my class Library :
public string custommessage(string messagetosend)
{
string receivedmessage = CallServer(messagetosend);
return receivedmessage;
}
and basicly when I am in a different program referencing the library I dont want to see this function in my list of avaiable functions to chose from :
Append
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
to your method as an attribute. This will hide the function from intellisense.

How to call a c library from unity3D C# code

I'm new to unity3D and C# (and IOS :), but need to get things working with a legacy C library (.h & .a files) on iPhone. I've read some about Plugins from unity documentation, but still feel overwhelmed by the complicated procedures. Could any guru show me the correct way out of the mess? Thanks!
Go through this link. This gives the idea for making plugins for iOS. If any doubt, ask.
A practical example for plugin
1) Make a C# file called AppControllerBinding.cs in the plugins folder in Unity and add the code as followed:
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
// All Objective-C exposed methods should be bound here
public class AppControllerBinding
{
[DllImport("__Internal")]
private static extern void _MyFunction(string myName);
public static void MyFunction(string MyNameIN)
{
// Call plugin only when running on real device
if( Application.platform == RuntimePlatform.IPhonePlayer )
_MyFunction(MyNameIN);
}
}
2) Add the MyFunction() function to the bottom on the AppController.mm file inside Xcode as follows (after the #end statement):
extern "C"
{
void _MyFunction(const char* MyName)
{
NSString* s = [NSString stringWithUTF8String: MyName];
[Self logName:s]; //<-----logName is method which takes string parameter
printf_console("_MyFunction() called in Appcontroller.mm in Xcode.\n");
}
}
3) When you want to use the logName function of AppController.mm inside Unity just make a call like this:
AppControllerBinding.MyFunction("Nick");

How can I restrict access to members in an assembly from Roslyn script engine?

I am experimenting with the Roslyn script engine. Using the following code, I set up my script engine.
var csharpEngine = new ScriptEngine();
csharpEngine.AddReference("System");
csharpEngine.AddReference(this.GetType().Assembly.Location);
scriptSession = csharpEngine.CreateSession();
Then I execute a script with the following line:
scriptSession.Execute(script);
The script contains a very simple reference to a static function on a class in my assembly.
private string script =
#"using System;
using RoslynWindow;
Hello.SayHello();";
In the output window the function merely prints to the console. So I have shown I can call into a public static member of my assembly without passing a "HostObjectModel" to the script engine. I want to prevent this from happening. I'd like to be able to register only specific members (functions, variables or properties) to be accessed by the script engine, and no others.
Any idea how to accomplish this?
Traverse the AST with Roslyn to check if the script tries to call anything you don't allow.

Loading a script from a cs file and accessing host methods, properties etc?

I'm just having a play with Roslyn but unsure on how to do the following.
To keep this simple, lets say I have a host program which has a method like so
public void DisplayMessage(string message)
{
MessageBox.Show(message);
}
Can I then have a script file called MyScript.csx and then somewhere in the script have something like
void Main()
{
Host.DisplayMessage("I am a script");
}
Then I have the host load the file and execute it.
If this sort of thing can't be done, is there a scripting system/engine based on c# that can do it?
These are the requirements
Host application can load script from a file.
Script file is written in c# and so can be written using VS2010 with syntax etc
Script file can access host public methods, properties etc
I wrote an Introduction to the Roslyn scripting API that covers most of what you're asking. The ScriptEngine type also has a ExecuteFile method that would be useful for what you're trying to do.
Disclaimer: I work for Microsoft on the Roslyn project.
Yes, you can do what you want using Roslyn.
First, create a public Host class that has a public DisplayMessage method (or use a existing class). Then create ScriptEngine, specifying the assembly that contains Host as a reference. After that, you can call ExecuteFile() on your file, with the Host object as another parameter:
var engine = new ScriptEngine(references: new[] { typeof(Host).Assembly });
engine.ExecuteFile("MyScript.csx", new Host());
The script files doesn't need any Main() method, and you call the method on the host object directly:
DisplayMessage("I am a script");

Categories

Resources