Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by The Boy · Oct 25, 2015 at 01:45 AM · texture2dpngoutlineminimapsetpixels

Outline on Texture2D

I'm creating a minimap, but I want to add a little outline effect, just like in this answer: http://answers.unity3d.com/questions/1036262/shaders-offset-texture-coordinates-by-a-single-pix.html

But without shaders, because this texture will be saved as a png file, then use it as a sprite inside a UI Image.

This is what I have done: alt text Looks good, but how can I add a simple 1 pixel outline inside the texture?

For extra details, here is my script:

 using UnityEngine;
 using System.Collections;
 using System.IO;
 
 [RequireComponent(typeof(Camera))]
 public class MiniMap : MonoBehaviour {
 
     private Camera cam;
 
     void Awake () {
         cam = GetComponent<Camera> ();
 
         CreateMiniMap ();
 
         Destroy (gameObject);
     }
 
     void CreateMiniMap () {
         RenderTexture rt = new RenderTexture (512, 512, 16, RenderTextureFormat.ARGB32); //Create a render texture
         Texture2D minimap = new Texture2D (rt.width, rt.height, TextureFormat.ARGB32, false);
 
         cam.targetTexture = rt; 
         cam.Render ();
         RenderTexture.active = rt;
 
         minimap.ReadPixels (new Rect (0, 0, rt.width, rt.height), 0, 0); //Render texture to Texture2D
         minimap.Apply ();
         RemoveColor (cam.backgroundColor, Color.clear, minimap); //Removes the background of the Texture2D
         FillImage (Color.black, minimap);
 
         cam.targetTexture = null;
         RenderTexture.active = null;
         DestroyImmediate (rt);
 
         if (!Directory.Exists (Application.dataPath + "/MiniMaps")) {
             Directory.CreateDirectory(Application.dataPath + "/MiniMaps");
         } //Creates a directory to save the minimap
 
         File.WriteAllBytes (Application.dataPath + "/MiniMaps/" + GameManager.map.name + "_Minimap.png", minimap.EncodeToPNG ());
     } //Save as a PNG file
 
     void RemoveColor (Color color, Color newColor, Texture2D tex) {
         Color[] pixels = tex.GetPixels (0, 0, tex.width, tex.height, 0);
 
         for (int i = 0; i < pixels.Length; i++) {
             if (pixels[i] == color)
                 pixels[i] = newColor;
         }
 
         tex.SetPixels (0, 0, tex.width, tex.height, pixels, 0);
         tex.Apply ();
     }
 
     void FillImage (Color fillColor, Texture2D tex) {
         Color[] pixels = tex.GetPixels (0, 0, tex.width, tex.height, 0);
         
         for (int i = 0; i < pixels.Length; i++) {
             if (pixels[i] != Color.clear)
                 pixels[i] = fillColor;
         }
         
         tex.SetPixels (0, 0, tex.width, tex.height, pixels, 0);
         tex.Apply ();
     }
 }


default-minimap.png (8.4 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by newfreeknowledge · Sep 29, 2018 at 05:43 PM

I solved a similar problem by drawing the "outline image" in a different color before drawing the image I want on top of it:

 var outline: int = 3; //how many pixels of outline
 var rect = new Rect(x,y,a,b);
 var rect_outline = new Rect(x-outline, y-outline, a+2*outline, b+2*outline);
 GUI.DrawTexture(rect_outline, image_outline, ScaleMode.StretchToFill);
 GUI.DrawTexture(rect, image, ScaleMode.StretchToFill);

image_outline is a copy of image, but its pixels are filled with the desired outline color. So you do something like:

 image_outline = Instantiate(image) as Texture2D;
 FillImage (Color.white, image_outline);

I know the response is late, but I only noticed the question today. Maybe it can help someone.

Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

32 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

Related Questions

How to check the Completion scenario for my drawing? 1 Answer

Changing dinamically a texture 0 Answers

SetPixels edge pixels 0 Answers

Hide part of image with a certain color 0 Answers

pixel perfect sprited converted from texture2d 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges