• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Djspun · Sep 10, 2019 at 12:38 PM · mapminimaprender texture

Releasing render texture that is set to be RenderTexture.active! (error on android only)

hey unity world i have 2 small problems one is the error and the other is that the map/minimap show up black

the error im getting in full:


Releasing render texture that is set to be RenderTexture.active! UnityEngine.RenderTexture:INTERNAL_CALL_Release(RenderTexture) UnityEngine.RenderTexture:Release() (at /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/TextureBindings.gen.cs:1246) NJG.MapRenderer:ConfigCamera() (at F:\Project Runescape\Assets\Ninjutsu Games\NJG MiniMap\Common\Scripts\Core\MapRenderer.cs:151) NJG.c__Iterator0:MoveNext() (at F:\Project Runescape\Assets\Ninjutsu Games\NJG MiniMap\Common\Scripts\Core\MapRenderer.cs:268) UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)


if (Application.isPlaying && cam.targetTexture != null) cam.targetTexture.Release(); line 151 in monodevelop


if (!Application.isPlaying || NJGMap.instance.renderMode == NJGMap.RenderMode.Dynamic) ConfigCamera(); line 268 in monodevelop


here is the script:

using NJG; using System.Collections; using UnityEngine;

/// /// Very basic game map -- renderer component. It's able to draw a map into a 2D texture. /// /// namespace NJG {

 //[ExecuteInEditMode]
 public class MapRenderer : MonoBehaviour
 {
     static MapRenderer mInst;

     /// <summary>
     /// Get instance.
     /// </summary>

     static public MapRenderer instance
     {
         get
         {
             if (mInst == null)
             {
                 mInst = GameObject.FindObjectOfType(typeof(MapRenderer)) as MapRenderer;
                 if (mInst == null)
                 {
                     GameObject go = new GameObject("_MapRenderer");
                     go.transform.parent = NJGMapMono.instance.transform;
                     // Isolate this camera to prevent any interference with other cameras
                     go.layer = LayerMask.NameToLayer("TransparentFX");
                     //go.hideFlags = HideFlags.HideInInspector;
                     mInst = go.AddComponent<MapRenderer>();
                 }
             }
             return mInst;
         }
     }

     /// <summary>
     /// Cached transform for speed.
     /// </summary>

     public Transform cachedTransform { get { if (mTrans == null) mTrans = transform; return mTrans; } }

     /// <summary>
     /// Cached camera for speed.
     /// </summary>

     public Camera cam
     {
         get
         {
             if (mCam == null)
             {
                 mCam = gameObject.GetComponent<Camera>();
             }

             if (gameObject.GetComponent<Camera>() == null)
             {
                 mCam = gameObject.AddComponent<Camera>();
             }

             return mCam;
         }
     }

     public int mapImageIndex = 0;

     Vector2 lastSize;
     Vector2 mSize;
     Transform mTrans;
     bool canRender = true;
     bool mGenerated = false;
     bool mWarning = false;
     bool mReaded = false;
     bool mApplied = false;
     float lastRender = 0;
     NJGMap map;
     Camera mCam;

     void Awake()
     {
         cam.useOcclusionCulling = false;

         Render();
     }

     void Start()
     {
         if (map.boundLayers.value == 0)
         {
             Debug.LogWarning("Can't render map photo. You have not choosen any layer for bounds calculation. Go to the NJGMiniMap inspector.", map);
             //NJGTools.DestroyImmediate(gameObject);
             return;
         }

         if (map.renderLayers.value == 0)
         {
             Debug.LogWarning("Can't render map photo. You have not choosen any layer for rendering. Go to the NJGMiniMap inspector.", map);
             //NJGTools.DestroyImmediate(gameObject);
             return;
         }

         map.UpdateBounds(); 

         ConfigCamera();

         //if (map.optimize) StartCoroutine(DelayedDestroy(gameObject, 2));
         //if(!Application.isPlaying) Render();
     }

if UNITY_EDITOR

    //private RenderTexture lastTexture = null;
     //float lastCheck;

     /*void Update()
     {
         if(cam.targetTexture != null && NJGMap.instance.generateMapTexture)
         {
             //lastCheck = Time.time + 1f;
             Map[] mps = GameObject.FindObjectsOfType<Map>();
             for (int i = 0, imax = mps.Length; i < imax; i++)
             {
                 Map mp = mps[i];
                 mp.SetTexture(cam.targetTexture);
             }
             //lastTexture = cam.targetTexture;
         }
     }*/

endif

     public void ConfigCamera()
     {
         if (map == null)
         { 
             map = NJGMap.instance;
         }

if UNITY_EDITOR

        //cam.targetTexture = Application.isPlaying ? null : UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Ninjutsu Games/NJG MiniMap/Common/Materials/MapPreview.renderTexture", typeof(RenderTexture)) as RenderTexture;
         //RenderTexture.active = cam.targetTexture;

endif

        if (Application.isPlaying && cam.targetTexture != null) cam.targetTexture.Release();

         Bounds bounds = map.bounds;
         //bounds.Expand(new Vector3(-bounds.extents.x, 0f, -bounds.extents.z));
         cam.depth = -100;
         cam.backgroundColor = map.cameraBackgroundColor;
         cam.cullingMask = map.renderLayers;
         cam.clearFlags = (CameraClearFlags)map.cameraClearFlags;
         cam.orthographic = true;



         float z = 0;
         //float n = 0;

         if (map.orientation == NJGMap.Orientation.XYSideScroller)
         {
             cam.farClipPlane = bounds.size.z * 1.1f;

             //n = bounds.extents.x / bounds.extents.y;
             //if (n < cam.aspect)
             z = bounds.extents.y;
             //else
             //z = bounds.extents.x / cam.aspect;

             cam.aspect = bounds.size.x / bounds.size.y;
         }
         else if (map.orientation == NJGMap.Orientation.XZDefault)
         {
             cam.farClipPlane = bounds.size.y * 1.1f;

             //n = bounds.extents.x / bounds.extents.z;
             //if (n < cam.aspect)
             z = bounds.extents.z;
             //else
             //    z = bounds.extents.x / cam.aspect;

             cam.aspect = bounds.size.x / bounds.size.z;
         }
         cam.farClipPlane = cam.farClipPlane * 5f;
         cam.orthographicSize = z;

         if (map.orientation == NJGMap.Orientation.XZDefault)
         {
             cachedTransform.eulerAngles = new Vector3(90f, 0, 0);
             //cachedTransform.position = new Vector3(bounds.max.x - bounds.extents.x, bounds.size.y, bounds.center.z);//-(Mathf.Abs(bounds.max.z) + Mathf.Abs(bounds.extents.z))
             if (map.mapResolution == NJGMap.Resolution.Double)
             {
                 for (int i = 0; i < 4; i++)
                 {
                     switch (i)
                     {
                         case 0:
                             cachedTransform.position = new Vector3(bounds.center.x - bounds.extents.x, (bounds.center.y + bounds.extents.y) + 1f, bounds.center.z - bounds.extents.z);
                             break;

                         case 1:
                             cachedTransform.position = new Vector3(bounds.center.x + bounds.extents.x, (bounds.center.y + bounds.extents.y) + 1f, bounds.center.z - bounds.extents.z);
                             break;

                         case 2:
                             cachedTransform.position = new Vector3(bounds.center.x + bounds.extents.x, (bounds.center.y + bounds.extents.y) + 1f, bounds.center.z + bounds.extents.z);
                             break;

                         case 3:
                             cachedTransform.position = new Vector3(bounds.center.x - bounds.extents.x, (bounds.center.y + bounds.extents.y) + 1f, bounds.center.z + bounds.extents.z);
                             break;
                     }
                     Debug.Log("cachedTransform.position " + cachedTransform.position + " / mapImageIndex " + mapImageIndex);

                     cam.enabled = true;
                     mapImageIndex = i;
                     //cam.Render();
                 }
             }
             else
             {
                 cachedTransform.position = new Vector3(bounds.max.x - bounds.extents.x, bounds.size.y * 2f, bounds.center.z);
                 cam.enabled = true;
                 //cam.Render();
             }
         }
         else
         {
             cachedTransform.eulerAngles = new Vector3(0, 0, 0);
             cachedTransform.position = new Vector3(bounds.max.x - bounds.extents.x, bounds.center.y, -((Mathf.Abs(bounds.min.z) + Mathf.Abs(bounds.max.z)) + 10));
         }
     }


     /*IEnumerator DelayedDestroy(UnityEngine.Object obj, float delay)
     {
         yield return new WaitForSeconds(delay);

         NJGTools.DestroyImmediate(obj);
     }*/

     /*bool ClearColor(object obj)
     {
         Color32[] list = obj as Color32[];
         newColors = new Color32[list.Length];

         int i = 0;
         int imax = list.Length;

         for (; i < imax; i++)
         {
             Color c = list[i];
             if (c == bgColor) c = Color.clear;
             newColors[i] = c;
         }
         done = true;
         return false;
     }*/

     IEnumerator OnPostRender()
     {
         if (!Application.isPlaying || NJGMap.instance.renderMode == NJGMap.RenderMode.Dynamic) ConfigCamera();

         // Can't re-generate the texture map is makeNoLongReadable flag is turned on.
         if (mGenerated && map.optimize && Application.isPlaying && !mWarning)
         {
             mWarning = true;
             Debug.LogWarning("Can't Re-generate the map texture since 'optimize' is activated");
             canRender = false;
         }
         else
         {
             if (canRender)
             {
                 if (map.mapTexture == null)
                 {
                     mSize = map.mapSize;
                     if (map.mapResolution == NJGMap.Resolution.Double) mSize = map.mapSize * 2;
                     map.mapTexture = new Texture2D((int)mSize.x, (int)mSize.y, (TextureFormat)map.textureFormat, map.generateMipmaps);
                     map.mapTexture.name = "_NJGMapTexture";
                     map.mapTexture.filterMode = map.mapFilterMode;
                     map.mapTexture.wrapMode = map.mapWrapMode;
                     map.mapTexture.hideFlags = HideFlags.DontSaveInBuild | HideFlags.DontSaveInEditor;
                     lastSize = mSize;
                 }

                 //if(NJGMap.instance.generateMapTexture) yield return new WaitForEndOfFrame();

                 if (!mReaded || !Application.isPlaying)
                 {
                     //mReaded = true;

                     // First take a screenshot from game view when camera is rendering.
                     if (map.generateMapTexture && canRender)
                     {
                         if (NJGMap.instance.renderMode != NJGMap.RenderMode.Once)
                         {
                             mSize = map.mapSize;
                             if (map.mapResolution == NJGMap.Resolution.Double) mSize = map.mapSize * 2;
                             if (mSize.x >= lastSize.x || mSize.y >= lastSize.y)
                             {
                                 lastSize = mSize;
                                 map.mapTexture.Resize((int)mSize.x, (int)mSize.y);
                             }
                         }

                         if (map.mapResolution == NJGMap.Resolution.Double)
                         {

                             Bounds bounds = map.bounds;
                             //bounds.Expand(new Vector3(-bounds.extents.x, 0f, -bounds.extents.z));
                             for (int i = 0; i < 4; i++)
                             {
                                 switch (i)
                                 {
                                     case 0:

                                         //cam.Render();
                                         map.mapTexture.ReadPixels(new Rect(0f, 0f, map.mapSize.x, map.mapSize.y), 0, 0, map.generateMipmaps);
                                         cachedTransform.position = new Vector3(bounds.center.x - bounds.extents.x, (bounds.center.y + bounds.extents.y) + 1f, bounds.center.z - bounds.extents.z);
                                         //yield return new WaitForEndOfFrame();
                                         //map.mapTexture.Apply(map.generateMipmaps, map.optimize);
                                         break;

                                     case 1:
                                         cachedTransform.position = new Vector3(bounds.center.x + bounds.extents.x, (bounds.center.y + bounds.extents.y) + 1f, bounds.center.z - bounds.extents.z);
                                         //cam.Render();
                                         map.mapTexture.ReadPixels(new Rect(0f, 0f, map.mapSize.x, map.mapSize.y), (int)map.mapSize.x, 0, map.generateMipmaps);
                                         //yield return new WaitForEndOfFrame();
                                         //map.mapTexture.Apply(map.generateMipmaps, map.optimize);
                                         break;

                                     case 2:
                                         cachedTransform.position = new Vector3(bounds.center.x + bounds.extents.x, (bounds.center.y + bounds.extents.y) + 1f, bounds.center.z + bounds.extents.z);
                                         //cam.Render();
                                         map.mapTexture.ReadPixels(new Rect(0f, 0f, map.mapSize.x, map.mapSize.y), (int)map.mapSize.x, (int)map.mapSize.y, map.generateMipmaps);
                                         //yield return new WaitForEndOfFrame();
                                         //map.mapTexture.Apply(map.generateMipmaps, map.optimize);
                                         break;

                                     case 3:
                                         cachedTransform.position = new Vector3(bounds.center.x - bounds.extents.x, (bounds.center.y + bounds.extents.y) + 1f, bounds.center.z + bounds.extents.z);
                                         //cam.Render();
                                         mReaded = true;
                                         map.mapTexture.ReadPixels(new Rect(0f, 0f, map.mapSize.x, (map.mapSize.y)), 0, (int)map.mapSize.y, map.generateMipmaps);
                                         //map.mapTexture.Apply(map.generateMipmaps, map.optimize);
                                         break;
                                 }
                                 Debug.Log("mapImageIndex " + i + " / map.mapSize " + map.mapSize + " / cachedTransform.position " + cachedTransform.position + " / mReaded " + mReaded);
                             }
                             /*if (mapImageIndex == 0)
                             {
                                 map.mapTexture.ReadPixels(new Rect(0f, 0f, map.mapSize.x, map.mapSize.y), 0, 0, map.generateMipmaps);
                             }
                             else if (mapImageIndex == 1)
                             {
                                 map.mapTexture.ReadPixels(new Rect(0f, 0f, map.mapSize.x, map.mapSize.y), (int)map.mapSize.x, 0, map.generateMipmaps);
                             }
                             else if (mapImageIndex == 2)
                             {
                                 map.mapTexture.ReadPixels(new Rect(0f, 0f, map.mapSize.x, map.mapSize.y), (int)map.mapSize.x, (int)map.mapSize.y, map.generateMipmaps);
                             }
                             else if (mapImageIndex == 3)
                             {
                                 mReaded = true;
                                 map.mapTexture.ReadPixels(new Rect(0f, 0f, map.mapSize.x, (map.mapSize.y)), 0, (int)map.mapSize.y, map.generateMipmaps);
                             }*/

                             //Debug.Log("mapImageIndex " + mapImageIndex + " / map.mapSize " + map.mapSize + " / cachedTransform.position " + cachedTransform.position + " / mReaded " + mReaded);
                         }
                         else
                         {                                
                             mReaded = true;
                             map.mapTexture.ReadPixels(new Rect(0f, 0f, map.mapSize.x, map.mapSize.y), 0, 0, map.generateMipmaps);
                         }
                     }
                     else
                     {
                         if (map.userMapTexture != null)
                             map.userMapTexture.ReadPixels(new Rect(0f, 0f, map.mapSize.x, map.mapSize.y), 0, 0, map.generateMipmaps);
                     }
                 }

                 yield return new WaitForEndOfFrame();

                 if (!mApplied)
                 {
                     mApplied = true;
                     if (map.generateMapTexture)
                     {
                         if (map.optimize)
                         {
                             map.mapTexture.Compress(true);
                             canRender = false;
                         }
                         map.mapTexture.Apply(map.generateMipmaps, map.optimize);
                     }
                     else
                     {
                         /*if (map.transparentTexture)
                         {
                             ClearColor(map.userMapTexture.GetPixels32());
                             map.userMapTexture.SetPixels32(newColors);
                         }*/
                         //if (map.userMapTexture != null && canRender)
                         if (!Application.isPlaying) map.userMapTexture.Apply(map.generateMipmaps, false);//map.optimize
                     }
                 }

                 //yield return new WaitForEndOfFrame();

                 if (canRender && !mGenerated)
                 {
                     if (Application.isPlaying)
                     {
                         mGenerated = true;
                     }

                     if (map.generateMapTexture)
                     {
                         if(Application.isPlaying) NJGMap.SetTexture(map.mapTexture);
                     }
                     else
                     {
                         NJGMap.SetTexture(map.userMapTexture);
                     }
                 }
                 if (cam.enabled && Application.isPlaying) cam.enabled = false;
             }
         }
         lastRender = Time.time + 1f;
     }

     /// <summary>
     /// Redraw the map's texture.
     /// </summary>

     public void Render()
     {
         if (map == null)
         {
             map = NJGMap.instance;
         }

         if (Time.time >= lastRender)
         {

if UNITY_EDITOR

            if (Application.isPlaying)
             {
                 cam.targetTexture = null;
                 lastRender = Time.time + 1f;
             }

endif

            mReaded = false;
             mApplied = false;
             mGenerated = false;
             mWarning = false;

             if (!map.optimize) canRender = true;

             if (map.mapSize.x == 0 || map.mapSize.y == 0)
             {
                 map.mapSize = new Vector2(Screen.width, Screen.height);
             }

             if (map.generateMapTexture)
             {
                 if (map.userMapTexture != null)
                 {
                     NJGTools.Destroy(map.userMapTexture);
                     map.userMapTexture = null;
                 }

                 /*if (map.mapTexture != null)
                 {
                     NJGTools.DestroyImmediate(map.mapTexture);
                     map.mapTexture = null;
                 }*/

                 if (map.mapTexture == null)
                 {
                     mSize = map.mapSize;
                     if (map.mapResolution == NJGMap.Resolution.Double) mSize = map.mapSize * 2;
                     map.mapTexture = new Texture2D((int)mSize.x, (int)mSize.y, (TextureFormat)map.textureFormat, map.generateMipmaps);
                     map.mapTexture.name = "_NJGMapTexture";
                     map.mapTexture.filterMode = map.mapFilterMode;
                     map.mapTexture.wrapMode = map.mapWrapMode;
                     lastSize = mSize;
                 }
             }
             else if (!Application.isPlaying)
             {
                 if (map.mapTexture != null)
                 {
                     NJGTools.DestroyImmediate(map.mapTexture);
                     map.mapTexture = null;
                 }

                 //if (map.userMapTexture != null)
                 //{
                 map.userMapTexture = new Texture2D((int)map.mapSize.x, (int)map.mapSize.y, (TextureFormat)map.textureFormat, map.generateMipmaps);
                 map.userMapTexture.name = "_NJGTempTexture";
                 map.userMapTexture.filterMode = map.mapFilterMode;
                 map.userMapTexture.wrapMode = map.mapWrapMode;
                 //}
             }


             ConfigCamera();
             cam.enabled = true;
             if (!Application.isPlaying) cam.Render();
         }
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

180 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Minimap rotation 1 Answer

Uncover mini/world map 0 Answers

How to create a dynamic map in hand of FPS like Far Cry 2 or Firewatch? 0 Answers

How to make a minimap by pixels without second camera 1 Answer

Assigning UV Map to model at runtime 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges