How to use Unity in a C# project - c#

I just started to use unity3d.
I create a small scene with a first view controller, a script etc.etc. But I want to know how I can load and play a scene in a C# code project (Visual Studio).
Let me explain.
I've already made a C# project, with many variable, interfaces etc.
How can I use an Unity libraries, and some methods in c# code to load and play an unity scene.
Is this kind of code exist ? like :
using System.Unity.Scene;
......
{
loadScene(myscene);
playScene(myscene);
...
}
....
?

While you can load Scene's from code in the Unity API (See Application.LoadLevel), You can't exactly use the Unity API outside of unity. While you may have some luck importing the library's, the actually functionality won't be there because of the way the Unity IDE/Editor works.
Might I suggest building your non-unity project into a library (Make sure you set the Target framework to 3.5 or below as Unity uses mono 2.0 libraries instead of .NET), and then using that in your unity project.
You may also wish to look at UnityVS, It's a hands down the best Extension for unity: It gives a great deal of extension and usability for those who prefer VisualStudios over MonoDev.

To load a Unity scene using C#:
using UnityEngine;
using System;
class YourClass
{
// ...
public void LoadScene( string sceneName )
{
Application.LoadLevel( sceneName );
}
// ...
}
http://docs.unity3d.com/Documentation/ScriptReference/Application.LoadLevel.html
But as others have mentioned, you should really start Googling some basic Unity C# tutorials.

I think you are looking at this the wrong way, Unity3D is a development environment, and provides much of the tools and structure you need to work within the framework they provide, the meta-data behind the scenes enables much of it's features, to link objects together from the scene to the C# classes.
My suggestion is to look at integrating other libraries into the Unity3D project, rather than the other way around, which will work fine.
I have done similar things before, such as integrating some serialization libraries and API's.
One option you could look at is to hook the Unity3D project up to a web service, and control things that way, allowing you to have some of that logic outside of the Unity3D project
I suggest here as a good start for learning Unity3D:
https://www.assetstore.unity3d.com/#/content/5087

Try using monodevelop instead. Unity integrates really well with Monodevelop and it comes packaged with unity.
In your editor on the top menu Assets->Create->C# Script
Double Click on your newly created C# script and it should open in monodevelop.
Then code away!
Every C# script should use
using UnityEngine;
To access the framework, this is usually set up automatically by default.
To load a scene:
Application.LoadLevel("Level Name");

Related

What's the equivalent of OpenDialog for a C#/Monogame app for Mac?

I've started to learn C# and Monogame a few weeks ago, and the project I'm working on to learn to use the framework is a simple game with a map made of arrays. Now that I have this working, I want to know what's the equivalent of OpenDialog but for macOS, as I'm coding a in-game map editor for my game (a very simple one) and I want to be able to open/write/load a JSON file where the data of the maps will be, on runtime.
If I was a windows user, I could have used OpenDialog but because I can't I'm wondering what to do. Is there a C# library of some kind that could help me? I would really like to not mix different languages (like using Swift and NSOpenPanel for example) because it's a pain and also because the only language I already know is Lua...
Thanks for helping! :)
Monogame does not have this functionality built in. Most games use a save slot feature instead of direct filesystem selection.
Map editors are usually done using winforms applications external to the game itself.
To do this task in Monogame, use File class and others in the System.IO namespace to Read and Write files to the filesystem. You must have write access to the folder. You will have to write your own user interface.
Depending on the state of Winforms implementation on Mac, you may be able to add a form to your game and use the FileDialog class directly.

Is it possible to use the managedCUDA wrapper to run parallel computations from a Unity C# script?

I'm trying to work out the best way to make calls to CUDA code from a Unity C# script.
The managedCUDA project seems the best way to wrap a C# interface to the CUDA kernels, but would this work if the C# script was compiled by Unity?
Any insight here would be grateful.
Thanks
Instead of trying to do something very complicated I would prefer to use a Computer Shader which is available and built in Unity. Compute shaders are basically programs that run on the graphics card, outside of the normal rendering pipeline. The only draw back is you need to know about shader programming (HLSL) which is not C# unfortuately. There is a nice tutorial here.
Cuda (and managedCUDA) use exactly the same approach, compile and run a program written in C (or C#) language in the GPU, using compute shaders.
Otherwise you can add external c# library to your project, just add the dlls from managedCUDA to you asset folder, and reference them in your solution. Then run your cuda code from the Start method of your Unity script.
Hope it helps.

Keep Unity from throwing build error about needing pro version to use specific feature I am not using

What I have found out so far
I am using C# and Unity to make a simple game. When I try and build this game for Android I get this error:
Error building Player: SystemException: 'System.Net.Sockets' are supported only with Unity Android Pro. Referenced from assembly 'Mono.Data.Tds'.
What this is saying is that I need to buy the Android Pro Licence to build a game with System.Net.Sockets used in it. This confused me at first because I did not remember using System.Net.Sockets at all. After some research I discovered that using to broad of a scope in a Unity C# project causes this error, in my case I was doing:
using System;
Which by proxy included System.Net.Sockets making Unity think I was using it.
The Issue
I am now using some open source library's in my game. When I went to build for Android after implementing these library's I got the same error. I then proceded to look at the source code for these library's and it seams that in every file the author(s) do:
using System;
Which was the cause of the error in the first part. These library's also do not make use of System.Net.Sockets but suffer from using to broad of a scope.
The question
Is there some way to have Unity recognize that I am not actually using System.Net.Sockets so it does not make me buy Android Pro?
Edit
After doing what #Roberto suggested I removed all my library's and then started adding them back in until I got the error. I have determined that Json.NET is the culprit and is doing something with System.Net.Sockets. However when I search the source for 'System.Net.Sockets' or 'Mono.Data.Tds' there are no results.
This is not right, adding using System won't expand to System.Net.Sockets. Doesn't make much sense and otherwise would make virtually every game impossible to run on Unity free.
The thing is your code or a plugin is using System.Net.Sockets. It may be a library (dll) perhaps, but definitely something is using it.
You can test my claims by creating a new project with one script with using System and see that you won't have problems; additionally you may want to create a new project and increasingly add the plugins you are using to check which one is using System.Net.Sockets.
I just read that this was happening on Unity 3.5.6. Are you using this version? If so, can't you update to Unity 4? I'm using Unity 4 and I don't have this problem. There's also 3.5.7 to try.

Linking C++ header and lib file with Unity

So i am creating a simple game in unity where i want to use the tn gaming vest.
http://tngames.com/pages/Developers
I have downloaded the SDK from their homepage, this SDK is a C++ header and library file. What I dont know is how to approach from here on, using this for my unity project. Anyone know how to couple the files with my game so I can use the methods?
One problem is that i dont have Unity pro so i cannot use plugins as some people have suggested on some questions i found.
At least since Unity 5, it is possible for both Pro and Free users to create native plugins, and interface with the platform they are running on:
Unity - Manual: Native Plugins
The steps are different for each platform, but they usually involve creating some kind of library (for instance dynamic .so under Android and static .a under iOs, which are the platforms I'm dealing with right now) and import the symbol into C# (everything is summarily explained in the documentation).
The process has it's own quirks, but it definitely works if enough effort is put into it...
Hope it helps (and removes some confusion that might be triggered by the, now outdated, accepted answer)
Without Unity pro you can't use plugins and thus you can't use native library or external code.

Javascript (or similar) gaming scripts for C# XNA

Lately I am preparing myself to develop yet another game in XNA C#.
The last time I worked on a game in XNA C#, I ran into this problem of having to add maps and customizable data into the game. Each time I want to add in new content or change some values to the game character or something, I had to rebuild the whole game or what - which can take quite some time.
Is there any way which I can write gaming scripts to load the content that can be interpreted by my XNA C# game without having to compile? I am looking at hopefully Javascript.
I would hope that I can add in functions and other small API which can be called from the external gaming script to manipulate the game or load data so on. I am not concerned about the game being hacked since it's quite early yet - the development is of much priority.
Any help would be greatly appreciated.
You can try Jint which is a open source Javascript interpreter written in C#
The compact framework doesn't include System.Reflection.Emit, which means you can't use IronPython. I recommend LUA, and you can find an XNA interpreter implementation with open source here
Would IronPython be more appropriate? That should work well on "full" framework - but it looks like it might not work (yet) on xbox 360 (which uses compact framework).
For an IronPython / xna example, see here.

Categories