Kishir
@Kishir

Unity — gmcs — Error running gmcs: Cannot find the specified file — Что делать?

faf48a5b3c924f43a7a997124a16c080.pngSystemException: Error running gmcs: Cannot find the specified file
Ошибка при запуске проекта.
Суть в следующем, нужно скомпилировать после запуска проекта код находящийся условно в переменной типа string.
Тестовый код:
using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Text;
using UnityEngine;

public class RuntimeCompileTest : MonoBehaviour
{
	void Start()
	{
		var assembly = Compile(@"
        using UnityEngine;

        public class RuntimeCompiled : MonoBehaviour
        {
            public static RuntimeCompiled AddYourselfTo(GameObject host)
            {
                return host.AddComponent<RuntimeCompiled>();
            }

            void Start()
            {
                Debug.Log(""The runtime compiled component was successfully attached to"" + gameObject.name);
            }
        }");    

		var runtimeType = assembly.GetType("RuntimeCompiled");
		var method = runtimeType.GetMethod("AddYourselfTo");
		var del = (Func<GameObject, MonoBehaviour>)
			Delegate.CreateDelegate(
				typeof(Func<GameObject, MonoBehaviour>), 
				method
			);

		// We ask the compiled method to add its component to this.gameObject
		var addedComponent = del.Invoke(gameObject);

		// The delegate pre-bakes the reflection, so repeated calls don't
		// cost us every time, as long as we keep re-using the delegate.
	}

	public static Assembly Compile(string source)
	{
		// Replace this Compiler.CSharpCodeProvider wth aeroson's version
		// if you're targeting non-Windows platforms:
		var provider = new CSharpCodeProvider();
		var param = new CompilerParameters();

		// Add ALL of the assembly references
		foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
		{
			param.ReferencedAssemblies.Add(assembly.Location);
		}

		// Or, uncomment just the assemblies you need...

		// System namespace for common types like collections.
		//param.ReferencedAssemblies.Add("System.dll");

		// This contains methods from the Unity namespaces:
		//param.ReferencedAssemblies.Add("UnityEngines.dll");

		// This assembly contains runtime C# code from your Assets folders:
		// (If you're using editor scripts, they may be in another assembly)
		//param.ReferencedAssemblies.Add("CSharp.dll");


		// Generate a dll in memory
		param.GenerateExecutable = false;
		param.GenerateInMemory = true;

		// Compile the source
		var result = provider.CompileAssemblyFromSource(param, source);

		if (result.Errors.Count > 0) {
			var msg = new StringBuilder();
			foreach (CompilerError error in result.Errors) {
				msg.AppendFormat("Error ({0}): {1}\n",
					error.ErrorNumber, error.ErrorText);
			}
			throw new Exception(msg.ToString());
		}

		// Return the assembly
		return result.CompiledAssembly;
	}
}


Выдает ошибку:
SystemException: Error running gmcs: Cannot find the specified file (на картинке)
Я пробовал устанавливать пакет MonoRuntime, пробовал делать ссылку в папку /usr/local/bin этого компилятора, ничего не помогает. В player установил .NET 2.0 как некоторые советуют. Ничего не помогает. Система OS X 10.11.6
Ошибка показывает что находится в строке:
var result = provider.CompileAssemblyFromSource(param, source);
Подскажите что мне делать?
  • Вопрос задан
  • 531 просмотр
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы