Code Monkey home page Code Monkey 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

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.