首页 > 解决方案 > 此脚本中的 python 是从哪里调用/提取的?

问题描述

我在 Unity 5 中找到了一个可以在 Unity 中运行 python 的项目,我想向其中添加库。问题是,我似乎无法从这个脚本中找到 python 库。在我看来,它们似乎是凭空产生的。我可以指出这可能来自哪里吗?我尝试将它添加到 python 路径,但它似乎不起作用。

using System;
using System.IO;
using System.Linq;
using UnityEngine;
using System.Text;
using System.Collections;
using IronPython.Hosting;
using IronPython.Modules;
using System.Collections.Generic;
using Microsoft.Scripting.Hosting;


/// <summary>
/// Interpreter for IronPython.
/// </summary>
public class Interpreter
{
    /// <summary>
    /// The scope.
    /// </summary>
    private ScriptScope  Scope;

    /// <summary>
    /// The engine.
    /// </summary>
    private ScriptEngine Engine;

    /// <summary>
    /// The source.
    /// </summary>
    private ScriptSource Source;

    /// <summary>
    /// The compiled.
    /// </summary>
    private CompiledCode Compiled;

    /// <summary>
    /// The operation.
    /// </summary>
    private ObjectOperations Operation;

    /// <summary>
    /// The python class.
    /// </summary>
    private object PythonClass;

    /// <summary>
    /// Initializes a new instance of the <see cref="Interpreter"/> class.
    /// </summary>
    public Interpreter()
    {
        Engine = Python.CreateEngine();
        Scope  = Engine.CreateScope();
        SetLibrary();
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="Interpreter"/> class.
    /// </summary>
    /// <param name="source">Source.</param>
    public Interpreter(string src) : this()
    {
        Compile(src);
    }

    /// <summary>
    /// Compile the specified src.
    /// </summary>
    /// <param name="src">Source.</param>
    public string Compile(string src, Microsoft.Scripting.SourceCodeKind CodeKind =
                                      Microsoft.Scripting.SourceCodeKind.SingleStatement)
    {
        if(src == string.Empty)
            return string.Empty;

        LoadRuntime();


        Source = CodeKind == Microsoft.Scripting.SourceCodeKind.SingleStatement ?
                             Engine.CreateScriptSourceFromString(src, CodeKind) :
                             Engine.CreateScriptSourceFromFile(src);

        ErrorHandle errors = new ErrorHandle();

        MemoryStream stream = new MemoryStream();
        //Set IO Ouput of execution
        Engine.Runtime.IO.SetOutput(stream, new StreamWriter(stream));

        Compiled  = Source.Compile(errors);
        Operation = Engine.CreateOperations();

        try {
            Compiled.Execute(Scope);
            return FormatOutput(ReadFromStream(stream));

        } catch(Exception ex) {
            return Engine.GetService<ExceptionOperations>().FormatException(ex);
        }
    }

    /// <summary>
    /// Formats the output of execution
    /// </summary>
    /// <returns>The output.</returns>
    /// <param name="output">Output.</param>
    private string FormatOutput(string output)
    {
        return string.IsNullOrEmpty(output) ? string.Empty 
        :      string.Join("\n", output.Remove(output.Length-1)
                                       .Split('\n')
                                       .Reverse().ToArray());
    }

    /// <summary>
    /// Reads MemoryStream.
    /// </summary>
    /// <returns>The from stream.</returns>
    /// <param name="ms">Ms.</param>
    private string ReadFromStream(MemoryStream ms) {

        int length = (int)ms.Length;
        Byte[] bytes = new Byte[ms.Length];
        ms.Seek(0, SeekOrigin.Begin);
        ms.Read(bytes, 0, length);

        return Encoding.GetEncoding("utf-8").GetString(bytes, 0, length);
    }

    /// <summary>
    /// Set sys paths
    /// </summary>
    private void SetLibrary()
    {
        if(PythonBase.SysPath.Count > 0) {

            ICollection<string> SysPath = Engine.GetSearchPaths();

            foreach(string path in PythonBase.SysPath)
                SysPath.Add(path);

            Engine.SetSearchPaths(SysPath);
        }
    }

    /// <summary>
    /// Load runtime Assemblies of Unity3D
    /// </summary>
    private void LoadRuntime()
    {
        Engine.Runtime.LoadAssembly(typeof(GameObject).Assembly);
    }

    public void AddRuntime<T>()
    {
        Engine.Runtime.LoadAssembly(typeof(T).Assembly);
    }

    public void AddRuntime(Type type)
    {
        Engine.Runtime.LoadAssembly(type.Assembly);
    }

    /// <summary>
    /// Gets the variable or class
    /// </summary>
    /// <returns>The variable.</returns>
    /// <param name="name">Name.</param>
    public object GetVariable(string name)
    {
        return Operation.Invoke(Scope.GetVariable(name));
    }

    /// <summary>
    /// Calls the method.
    /// </summary>
    /// <param name="name">Name.</param>
    public void InvokeMethod(object nameClass, string Method, params object[] parameters)
    {
        object output = new object();
        if(Operation.TryGetMember(nameClass, Method, out output)) {
            object Func = Operation.GetMember(nameClass, Method);
            Operation.Invoke(Func, parameters);
        }
    }
}

标签: c#pythonunity3d

解决方案


推荐阅读