简体   繁体   中英

How to use Unity in a C# project

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM