Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Build Error with script

Discussion in 'Scripting' started by RemDust, Jan 2, 2016.

  1. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    431
    Hi guys, I use a script (sorry I lost the name's author, shame on me) to be able to set the render order of meshes. I use it to be able to play video clip in my 2Dgame. It all works fine in editor but give me errors when trying to build and run.
    Here is the code :

    usingUnityEngine;
    usingUnityEditor;
    usingSystem.Collections;
    usingUnityEditorInternal;
    usingSystem.Reflection;
    usingSystem;
    [CanEditMultipleObjects]
    [CustomEditor(typeof(MeshRenderer))]
    publicclassVideoRenderer : Editor
    {
    publicoverridevoidOnInspectorGUI()
    {
    base.OnInspectorGUI();
    serializedObject.Update();
    SerializedPropertysortingLayerID = serializedObject.FindProperty("m_SortingLayerID");
    SerializedPropertysortingOrder = serializedObject.FindProperty("m_SortingOrder");
    //MeshRendererrenderer = targetasMeshRenderer;
    RectfirstHoriz = EditorGUILayout.BeginHorizontal();
    EditorGUI.BeginChangeCheck();
    EditorGUI.BeginProperty(firstHoriz, GUIContent.none, sortingLayerID);
    string[] layerNames = GetSortingLayerNames();
    int[] layerID = GetSortingLayerUniqueIDs();
    intselected = -1;
    intsID = sortingLayerID.intValue;
    for (inti = 0; i < layerID.Length; i++)
    if (sID == layerID)
    selected = i;
    if (selected == -1)
    for (inti = 0; i < layerID.Length; i++)
    if (layerID == 0)
    selected = i;
    selected = EditorGUILayout.Popup("Sorting Layer", selected, layerNames);
    sortingLayerID.intValue = layerID[selected];
    EditorGUI.EndProperty();
    EditorGUILayout.EndHorizontal();
    EditorGUILayout.BeginHorizontal();
    EditorGUI.BeginChangeCheck();
    EditorGUILayout.PropertyField(sortingOrder, newGUIContent("Order in Layer"));
    EditorGUILayout.EndHorizontal();
    serializedObject.ApplyModifiedProperties();
    }
    publicstring[] GetSortingLayerNames()
    {
    TypeinternalEditorUtilityType = typeof(InternalEditorUtility);
    PropertyInfosortingLayersProperty = internalEditorUtilityType.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic);
    return (string[])sortingLayersProperty.GetValue(null, newobject[0]);
    }
    publicint[] GetSortingLayerUniqueIDs()
    {
    TypeinternalEditorUtilityType = typeof(InternalEditorUtility);
    PropertyInfosortingLayerUniqueIDsProperty = internalEditorUtilityType.GetProperty("sortingLayerUniqueIDs", BindingFlags.Static | BindingFlags.NonPublic);
    return (int[])sortingLayerUniqueIDsProperty.GetValue(null, newobject[0]);
    }
    }

    Errors :
    The type or namespace name `UnityEditorInternal' could not be found. Are you missing a using directive or an assembly reference?
    The type or namespace name `UnityEditor' could not be found. Are you missing a using directive or an assembly reference?

    I guess this is because when built it runs out of my project folders or something, but what can i do ??Thank you all.
     
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    Last edited: Jan 2, 2016
  3. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    431
    Oh...Does that mean that my code won't have ANY effect outside of the editor ?!
    If yes I really don't see the point of this script which was made to be able to customize render of videoclips on meshes !
     
  4. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    I'm afraid so.
    its' hard to read, but i think it's a custom editor to adjust properties on a variable called "serializedObject" .
    could you please edit your post and use code tags... then your code could be tested to see what it does :)
     
  5. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    431
    Yeah sure, sorry :)

    Code (csharp):
    1.  
    2. usingUnityEngine;
    3. usingUnityEditor;
    4. usingSystem.Collections;
    5. usingUnityEditorInternal;
    6. usingSystem.Reflection;
    7. usingSystem;
    8. [CanEditMultipleObjects]
    9. [CustomEditor(typeof(MeshRenderer))]
    10. publicclassVideoRenderer : Editor
    11. {
    12. publicoverridevoidOnInspectorGUI()
    13. {
    14. base.OnInspectorGUI();
    15. serializedObject.Update();
    16. SerializedPropertysortingLayerID = serializedObject.FindProperty("m_SortingLayerID");
    17. SerializedPropertysortingOrder = serializedObject.FindProperty("m_SortingOrder");
    18. //MeshRendererrenderer = targetasMeshRenderer;
    19. RectfirstHoriz = EditorGUILayout.BeginHorizontal();
    20. EditorGUI.BeginChangeCheck();
    21. EditorGUI.BeginProperty(firstHoriz, GUIContent.none, sortingLayerID);
    22. string[] layerNames = GetSortingLayerNames();
    23. int[] layerID = GetSortingLayerUniqueIDs();
    24. intselected = -1;
    25. intsID = sortingLayerID.intValue;
    26. for (inti = 0; i < layerID.Length; i++)
    27. if (sID == layerID)
    28. selected = i;
    29. if (selected == -1)
    30. for (inti = 0; i < layerID.Length; i++)
    31. if (layerID == 0)
    32. selected = i;
    33. selected = EditorGUILayout.Popup("Sorting Layer", selected, layerNames);
    34. sortingLayerID.intValue = layerID[selected];
    35. EditorGUI.EndProperty();
    36. EditorGUILayout.EndHorizontal();
    37. EditorGUILayout.BeginHorizontal();
    38. EditorGUI.BeginChangeCheck();
    39. EditorGUILayout.PropertyField(sortingOrder, newGUIContent("Order in Layer"));
    40. EditorGUILayout.EndHorizontal();
    41. serializedObject.ApplyModifiedProperties();
    42. }
    43. publicstring[] GetSortingLayerNames()
    44. {
    45. TypeinternalEditorUtilityType = typeof(InternalEditorUtility);
    46. PropertyInfosortingLayersProperty = internalEditorUtilityType.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic);
    47. return (string[])sortingLayersProperty.GetValue(null, newobject[0]);
    48. }
    49. publicint[] GetSortingLayerUniqueIDs()
    50. {
    51. TypeinternalEditorUtilityType = typeof(InternalEditorUtility);
    52. PropertyInfosortingLayerUniqueIDsProperty = internalEditorUtilityType.GetProperty("sortingLayerUniqueIDs", BindingFlags.Static | BindingFlags.NonPublic);
    53. return (int[])sortingLayerUniqueIDsProperty.GetValue(null, newobject[0]);
    54. }
    55. }
    56.  
    So yes, it basically let me adjust in the inspector the sorting layer for the mesh component.
    This is awesome for 2D work, but I really don't see the point of such an "advanced script" if it only works in editor...
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    When you use code tags, make sure to have basic things like indentation and spaces between words. This is as readable as without code tags, meaning not readable at all!
     
  7. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    431
    Damn, sorry again, I don't post long code so often and I don't really now why the spaces hadn't been respected here -_-


    Code (csharp):
    1.  
    2.  
    3. usingUnityEngine;
    4. usingUnityEditor;
    5. usingSystem.Collections;
    6. usingUnityEditorInternal;
    7. usingSystem.Reflection;
    8. usingSystem;
    9. [CanEditMultipleObjects]
    10. [CustomEditor(typeof(MeshRenderer))]
    11. public class VideoRenderer : Editor
    12. {
    13. public override void OnInspectorGUI()
    14.  {
    15. base.OnInspectorGUI();
    16. serializedObject.Update();
    17. SerializedProperty sortingLayerID = serializedObject.FindProperty("m_SortingLayerID");
    18. SerializedProperty sortingOrder = serializedObject.FindProperty("m_SortingOrder");
    19. //MeshRendererrenderer = targetasMeshRenderer;
    20. Rect firstHoriz = EditorGUILayout.BeginHorizontal();
    21. EditorGUI.BeginChangeCheck();
    22. EditorGUI.BeginProperty(firstHoriz, GUIContent.none, sortingLayerID);
    23. string[] layerNames = GetSortingLayerNames();
    24. int[] layerID = GetSortingLayerUniqueIDs();
    25. int selected = -1;
    26. int sID = sortingLayerID.intValue;
    27. for (inti = 0; i < layerID.Length; i++)
    28. if (sID == layerID[i])
    29. selected = i;
    30. if (selected == -1)
    31. for (inti = 0; i < layerID.Length; i++)
    32. if (layerID[i] == 0)
    33. selected = i;
    34. selected = EditorGUILayout.Popup("Sorting Layer", selected, layerNames);
    35. sortingLayerID.intValue = layerID[selected];
    36. EditorGUI.EndProperty();
    37. EditorGUILayout.EndHorizontal();
    38. EditorGUILayout.BeginHorizontal();
    39. EditorGUI.BeginChangeCheck();
    40. EditorGUILayout.PropertyField(sortingOrder, newGUIContent("Order in Layer"));
    41. EditorGUILayout.EndHorizontal();
    42. serializedObject.ApplyModifiedProperties();
    43.  }
    44. public string [] GetSortingLayerNames()
    45.  {
    46. Type internalEditorUtilityType = typeof(InternalEditorUtility);
    47. PropertyInfo sortingLayersProperty = internalEditorUtilityType.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic);
    48. return (string[])sortingLayersProperty.GetValue(null, newobject[0]);
    49.  }
    50. public int [] GetSortingLayerUniqueIDs()
    51.  {
    52. Type internalEditorUtilityType = typeof(InternalEditorUtility);
    53. PropertyInfo sortingLayerUniqueIDsProperty = internalEditorUtilityType.GetProperty("sortingLayerUniqueIDs", BindingFlags.Static | BindingFlags.NonPublic);
    54. return (int[])sortingLayerUniqueIDsProperty.GetValue(null, newobject[0]);
    55.  }
    56. }
    57.  
    58.  
     
  8. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    Almost!! your namespaces are still a bit messed up... but getting there :)

    anyway what is, or where do you get serializedObject, because the purpose of this script looks like making adjustments in SerializedObject as end result.
     
  9. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    431
    To be honest, I don't really understand this script, which is why I don't know how to get rid off my problem i guess ^^
    But it doesn't rely on any other "custom" scripts, as far as I understand (or use it, should I say) it just adds some properties in my Mesh Renderer component.
    That make me able to set the sorting layer (see image)
     

    Attached Files:

  10. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    right now that i had a better look, it's a custom editor for a MeshRenderer so that's you serialzedObject.
    anyway, is the script in the editor folder? then unity should know what to do with it when compiling.

    maybe this helps understanding the script better.
     
    Last edited: Jan 2, 2016
  11. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    431
    thank you dude, I'm gonna check it out !
    and no, the script was in a simple folder in assets :)
     
  12. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    431
    Well, I placed this script in an Editor folder and it seems to work.
    BUT ! ^^
    Does that mean that it will be considered by other platforms when build ? I mean, the official description of Editor Folder seems to indicate that it shouldn't be processed outside of the editor... kinda confusing !
    Anyway, for now this seems to do the trick, thank you @jister ;)
     
  13. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    @RemDust sorry that was the first thing i should have asked, no you see the horrors of not using code tags :D
    anyway here, if you didn't read that already.
     
    RemDust likes this.