3

While developing the game, I need a method that changes the rendering type of the material, but the corresponding method is in the internal class StandardShaderGUI, which I don't have access to. I wanted to just copy the code, but I saw the Unity Reference-Only License, after reading the text of this license I still don't understand whether I can copy some code from the repository if I change it a bit or not?

https://github.com/Unity-Technologies/UnityCsReference

License - https://unity3d.com/legal/licenses/Unity_Reference_Only_License

2 Answers 2

7

The licence seems to me pretty unambiguous when in s1 it says

For clarity, while display, performance, and reproduction on the Repository Service through its functionality is allowed, no other distribution or modification of the Software is permitted.

So you're not allowed to copy some code from the repository whether you modify it or not. As I read it, this software is source-visible but non-free, and should be avoided.

2
  • Thank you for the answer, how can I avoid using this code if there is only one way to perform the operation I need?
    – UNICODE_
    May 23, 2022 at 12:02
  • 3
    As long as you haven't read the licensed code, just rewrite it. If you have, the safest way is the clean room reimplementation - you (having read the licensed code) write a functional specification of what it should do, then give that to another project member (who hasn't seen the licensed code) to implement.
    – MadHatter
    May 23, 2022 at 12:21
0

As MadHatter said, the license pretty clearly says that you aren't allowed to modify that source code, nor do anything else that doesn't involve just lookin at it.

As for the root of your question (access to an Internal class), the best solution would be to use Reflection. For your specific case, you could do something like this to call the AssignNewShaderToMaterial method:

Assembly thisAssembly = Assembly.GetCallingAssembly();
BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic;
object? standardShaderGUIInstance = thisAssembly.CreateInstance("UnityEditor.StandardShaderGUI",
    true, bindingFlags, null, null, null, null);

if (standardShaderGUIInstance == null)
{
    Debug.Log("Could not find the StandardShaderGUI class. Aborting!");
    return;
}

Type standardShaderGUIType = standardShaderGUIInstance.GetType();
MethodInfo? standardShaderGUIMethodInfo = standardShaderGUIType.GetMethod("AssignNewShaderToMaterial");

if (standardShaderGUIMethodInfo == null)
{
    Debug.Log("Could not find the AssignNewShaderToMaterial method. Aborting!");
    return;
}

object[] parameters = {material, oldShader, newShader};
standardShaderGUIMethodInfo.Invoke(standardShaderGUIInstance, parameters);

Also, I assume you're trying to run your code in the Editor. If you're trying to access this class in a built version of the game, it likely doesn't exist because it's part of the UnityEditor namespace.

If you need any more help, it might be best to ask on StackOverflow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.