Calling C# Unity Script from Android - c#

I have been trying to communicate with my Unity C# script through my Android Application using
"UnityPlayer.UnitySendMessage("Cube", "Test","HELLO") - where "Cube" is my Unity Object, "Test" is the name of the method present in the C# script it is using and "HELLO" is the message String I want to pass.
This line is placed in my onClick function, like this:
ImageButton right_button = (ImageButton) findViewById(R.id.right_arrow);
right_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
UnityPlayer.UnitySendMessage("Cube", "Test","HELLO");
}
});
But when I run my android application and as soon as I click this button, I get this error:
09-11 14:22:37.526: E/AndroidRuntime(1330): java.lang.NoClassDefFoundError: com.unity3d.player.UnityPlayer
I have included classes.jar in my build path also.
Is there anything else I am missing out?
Thanks in advance!

Check if the class.jar file is in the Build Path of your Project. And if so, check if it is selected (check box is marked).
Tip: if you get ClassDefNotFound runtime errors (or similar), you might need to perform this extra step:
go to Project > Properties > Java Build Path -> Order and Export tab and check (tick) the classes.jar, Android x.y and Android Dependencies items; then Apply and rebuild the probject.

Happened to me the other time too.
I clean, re-link and rebuild my eclipse project. Re-Export my jar file to unity and also restart Unity. Build again and the error is gone.

Related

Unity does not even recognize "Collider" on my function "OnTriggerEnter()"

I'm a Unity beginner ,so right now I'm doing some small projects by watching some tutorials on youtube.
But, after looking at one tutorial that used the "OnTriggerEnter" function with the attribute "Collider". I realized that my editor didn't recognize "Collider" so I couldn't use "OnTriggerEnter".
I searched on the internet, but coudln't find any answer related to my question.
It's the first time, it's happened to me so I don't really know how to solve this problem...
Here's my little code, I'm just trying to move something when my player enters in a zone.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Trigger : MonoBehaviour
{
public bool opening = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
// OnTriggerEnter is called when something enter in the trigger
void OnTriggerEnter(Collider obj)
{
if(obj.transform.name == "Player")
{
opening = true;
}
}
}
So, as I said, my unity editor which is Microsoft visual studio does not detect "Collider" (I don't have the possibility to pre-fill by pressing enter for example) and the color is white and not blue like "true" for example.
So because of that I can't move forward with the project, and I'd like to know what to do so that my editor recognizes "Collider" and I can make my project work!
Thank you in advance for your answers.
To solve that you can do the following:
Close Unity and Visual Studio.
Open Unity.
Open Visual Studio from Unity (Assets => Open C# Project).
If that doesn't work you need to check your current editor in:
Edit => Preferences => External Tools => External Script Editor
Visual Studio always have some errors like missing dependencies and stuff. I would recommend using Visual Studio Code application. Just like #Jack has explained. Browse to Edit => Preferences => External Tools => External Script Editor and change your script editor to Visual Studio Code and then try again.

Unable to connect Unity to Visual Studio

I am trying out Unity for my Game Design course, but I can't seem to get the code to work. I suspect it is because Visual Studio (which I write my code on) is not connecting to Unity properly, and here's why I think this:
For one thing, whenever I try to put the code on an object, it prints out this error:
Can't add script behaviour CallbackExecutor. The script needs to derive from MonoBehaviour!
This is despite the fact that nowhere is the code called CallbackExecutor, and the script apparently does derive from MonoBehaviour.
Second, when I load up the code onto Visual Studio, I get this error message:
C:\Users\cemya\Documents\D&D Roguelight Project\Assembly-CSharp.csproj : error : The project file could not be loaded. Could not find file 'C:\Users\cemya\Documents\D&D Roguelight Project\Assembly-CSharp.csproj'. C:\Users\cemya\Documents\D&D Roguelight Project\Assembly-CSharp.csproj
The problem is I don't know why it's not connecting. There's no option to import the package (which implies it's already imported), and I do have a package for Unity.
I'm using Visual Studios 2017 (specifically, I'm using the 2d setup most of the time), Unity version 2018.2.4f1, and the package is called Visual Studio 2017 Tools for Unity [Experimental], which is what was automatically downloaded when I began to set up coding for Unity. Since I got this version automatically, I believe that this should be a common problem, but I can't seem to find real answers on it (I even asked on the Unity forms themselves!)
I can show you the code if you think that would help.
UPDATE: I have just updated Unity to 2018.2.5f1 and tested it with the code that has actual code. All that happened is that the name for the error message is different:
Can't add script behaviour TMP_CoroutineTween. The script needs to derive from MonoBehaviour!
Also, here is the code for the one I'm testing, if it helps:
public class RollScript : MonoBehaviour
{
Random rand = new Random();
int r = 20;
int m = 5;
// Use this for initialization
void Start ()
{
int rolling = DieRoll(r, m);
print(rolling);
}
// Update is called once per frame
void Update ()
{
}
int DieRoll (int roll, int mod)
{
int get = rand.next(1, roll);
int result = get + mod;
return result;
}
}
UPDATE 2: I have checked with visual studio, and the error message there isn't there anymore, so that's an improvement.
UPDATE 3: I have created a new project and I have not run into issues there. I guess the problem was the code's connection to older editions.
This Can't add script behaviour TMP_CoroutineTween. The script needs to derive from MonoBehaviour! error has been mentioned in a few places on the internet, but with no satisfactory answers. This seems to be the only StackOverflow question about it.
I suspect it happens when there are un-fixed compile-time errors while you're adding a script to a GameObject. I had this problem just now, and after I fixed the errors, I was able to add the script. However, I tried to confirm this by adding a syntax error and then adding a script, but I still didn't get the Can't add script behavior error. So that's odd, but if you're encountering that error, I suggest fixing any compile-time errors and see if that works. (Whether it works or not, please reply to this question with the result.)

How to integrate the unity ios project into native ios swift project?

I've tried most of the online resources including this link too to integrate my unity project into swift 4 native project but unfortunately not helped until now. When I do Linked fix I got into below c++ file error:
No member named 'GetHostByAddr40' in 'il2cpp::icalls::System::System::Net::Dns'; did you mean 'GetHostByAddr'?
Btw I am actually doing a augmented reality project which having some native iOS designs too. Since so I've done those parts using swift 4 with Xcode 9.2. The native part contains the project's start and the end part. In between that, there is augmented reality part which is done by using unity 2018.2.0f2 personal and vofuria 7.2.23.
In the end, now I have an iOS native project and unity project. I wanted to integrate the unity part into iOS native!
After a long search, I get ride the error.
After fixing with this described solutions, I've ended up with Bulk_Mono.Security_1.cpp file not found. So I've started to search for it. Then I've got identified the problem. The problem is with 4.0 Scripting runtime version of .Net. I made the project with 4.0 because of the use of LitJson requires min 4.0. The 4.0 is not generating the Bulk_Mono.Security_1.cpp file for iOS. Then I've removed the LitJson from the project and downgraded the project runtime version to 3.x Equivalent and it started to work fine with the changes on the following 2 files.
Note that just integrate the generated objective-c project as like a framework usually then make following changes and run.
made the change following on DisplayManager.mm generated file which is located in classes/unity. The auto-generated not contains the last line of return deviceUnknown;. So we have to write manually.
elif PLATFORM_TVOS
if (!strncmp(model, "AppleTV5,", 9))
return deviceAppleTV1Gen;
else if (!strncmp(model, "AppleTV6,", 9))
return deviceAppleTV2Gen;
else
return deviceUnknown;
endif
return deviceUnknown
Do the following change on SplashScreen.mm which is located in generated project's classes/ui. Change the bool hasStoryboard = [[NSBundle mainBundle] pathForResource: #"LaunchScreen" ofType: #"storyboardc"] != nullptr; to bool hasStoryboard = [[NSBundle mainBundle] pathForResource: #"LaunchScreen_1" ofType: #"storyboardc"] != nullptr;. The complete method below.
void ShowSplashScreen(UIWindow* window)
{
bool hasStoryboard = [[NSBundle mainBundle] pathForResource: #"LaunchScreen_1" ofType: #"storyboardc"] != nullptr;
if (hasStoryboard)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
#"LaunchScreen" bundle: [NSBundle mainBundle]];
_controller = [storyboard
instantiateViewControllerWithIdentifier: #"unitySplashStoryboard"];
}
else
_controller = [[SplashScreenController alloc] init];
[_controller create: window];
[window makeKeyAndVisible];
}
That's all about how I integrated.

Register functions without using them "Object reference not set to an instance of an object."

I have an IRC Bot in C# and I want to use Lua Scripting for the moment. On bot startup I want to register functions, and detect if a new file has been added then load it. I did the new file / reload scripts function already, but when I hit run I get "Object reference not set to an instance of an object." on a custom function I want users to be able to use.
Here's the current code:
public Lua lua;
public void RegisterFunctions() {
lua.RegisterFunction("print", this, typeof(DashLua).GetMethod("ConsoleOut"));
}
#region Custom Functions for Lua
public void ConsoleOut(String line) {
if (line == null) {
Console.WriteLine("Script error: print() can't be null.");
} else {
Console.WriteLine(line);
}
}
In the Main() of the bot I have just 2 lines currently:
DashLua dash = new DashLua();
dash.RegisterFunctions();
NLua (NuGet) only works on x64 builds which is why none of this actually worked. So if you're building a project and really need a Lua as a scripting language, you need to change your build to x64 in the Project's properties. That being said, you can get the Win32 version at their website which is a shame it's not on nuget.
EDIT: When you go and download the Win32 build, it's empty. I guess they have not built it and encourage you to build it yourself.

How to load referenced script libraries from a Roslyn script file?

I figured out I cannot load one script library from another easily:
module.csx
string SomeFunction() {
return "something";
}
script.csx
ExecuteFile("module.csx");
SomeFunction() <-- causes compile error "SomeFunction" does not exist
This is because the compiler does not know of module.csx at the time it compiles script.csx afaiu. I can add another script to load the two files from that one, and that will work. However thats not that pretty.
Instead I like to make my scripthost check for a special syntax "load module" within my scripts, and execute those modules before actual script execution.
script.csx
// load "module.csx"
SomeFunction()
Now, with some basic string handling, I can figure out which modules to load (lines that contains // load ...) and load that files (gist here https://gist.github.com/4147064):
foreach(var module in scriptModules) {
session.ExecuteFile(module);
}
return session.Execute(script)
But - since we're talking Roslyn, there should be some nice way to parse the script for the syntax I'm looking for, right?
And it might even exist a way to handle module libraries of code?
Currently in Roslyn there is no way to reference another script file. We are considering moving #load from being a host command of the Interactive Window to being a part of the language (like #r), but it isn't currently implemented.
As to how to deal with the strings, you could parse it normally, and then look for pre-processor directives that are of an unknown type and delve into the structure that way.
Support for #load in script files has been added as of https://github.com/dotnet/roslyn/commit/f1702c.
This functionality will be available in Visual Studio 2015 Update 1.
Include the script:
#load "common.csx"
...
And configure the source resolver when you run the scripts:
Script<object> script = CSharpScript.Create(code, ...);
var options = ScriptOptions.Default.WithSourceResolver(new SourceFileResolver(new string[] { }, baseDirectory));
var func = script.WithOptions(options).CreateDelegate()
...

Categories