Coder Social home page Coder Social logo

unity-python-demo's Introduction

Use Python with Unity

First you will have to download IronPython dll files and place them into Assets/Plugins directory. unity-python

IronPython requires .NET 4.5, and Unity doesn't like this by default. That's why you will have to change the .NET version by going to Edit --> Project Settings --> Player. Make sure that the Scripting Runtime Version is .NET 4.x (this requires you to restart Unity) and that API Compatibility level is .NET 4.x

Requirements

How to code in Python

Assume you have a small code snippet test.py in Python like this:

import random

class Test():
	def __init__(self, name):
		self.name = name

	def display(self):
		return "Hi, " + self.name

	def random_number(self, start, end):
		return random.randint(start, end)

You can use it from C# like this

var engine = Python.CreateEngine ();
ICollection<string> searchPaths = engine.GetSearchPaths ();

//Path to the folder of greeter.py
searchPaths.Add (Application.dataPath);

//Path to the Python standard library
searchPaths.Add (Application.dataPath + @"\StreamingAssets"  + @"\Lib\");
engine.SetSearchPaths (searchPaths);

dynamic py = engine.ExecuteFile (Application.dataPath + @"\StreamingAssets" + @"\Python\test.py");

dynamic test = py.Test ("Codemaker");
greeting.text = "Greeting: " + test.display ();
randomNumber.text = "Random Number: " + test.random_number (1, 5);

Output

screenshot

unity-python-demo's People

Contributors

codemaker2015 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

unity-python-demo's Issues

ImportException: No module named _______

Hello,

Your method works very well to use Python in Unity. But since my python script requires several module imports, I get an ImportException when I hit Play on Unity. For example:
ImportException: No module named serial
Microsoft.Scripting.Runtime.LightExceptions.ThrowException (Microsoft.Scripting.Runtime.LightExceptions+LightException lightEx) (at :0)
Microsoft.Scripting.Runtime.LightExceptions.CheckAndThrow (System.Object value) (at :0)
Microsoft.Scripting.Interpreter.FuncCallInstruction2[T0,TRet].Run (Microsoft.Scripting.Interpreter.InterpretedFrame frame) (at <bdde1939ff4d4d6b828ba549cdd24694>:0) Microsoft.Scripting.Interpreter.Interpreter.Run (Microsoft.Scripting.Interpreter.InterpretedFrame frame) (at <bdde1939ff4d4d6b828ba549cdd24694>:0) Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet] (T0 arg0, T1 arg1) (at <bdde1939ff4d4d6b828ba549cdd24694>:0) IronPython.Compiler.PythonCallTargets.OriginalCallTarget1 (IronPython.Runtime.PythonFunction function, System.Object arg0) (at <0569a20e5dd94f74a766cc11c6214b7c>:0) IronPython.Runtime.FunctionCaller1[T0].Call1 (System.Runtime.CompilerServices.CallSite site, IronPython.Runtime.CodeContext context, System.Object func, T0 arg0) (at <0569a20e5dd94f74a766cc11c6214b

Any ideas?

Thanks.

UnauthorisedAccessException on Android

Thank you for sharing this package. it works well on editor. but when i built it for android and run application in android device it gives.
"UnauthorisedAccessException: Access to path "/" is denied."

it is strange because file is present at this path and i can read the file via read all text. but it gives me error when i pass the same file path to "ExecuteFile()"

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.