• 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 AdisonCavani · May 10, 2021 at 05:35 PM · texture

2D Texture outline using SetPixel()

Hi, I've got a script, which is drawing a customizable crosshair (like in CS:GO) using Texture2D and SetPixel(). There is only one thing that is missing - outline. How to create an outline for this crosshair? Ofc the size needs to be customizable.

 using UnityEngine;
  
 [ExecuteInEditMode]
 public class Crosshair : MonoBehaviour
 {
     public Texture2D crosshairTex { get; private set; } // Crosshair texture
  
     [Range(0f, 50f)] public uint size = 14; // Crosshair size
     [Range(0f, 10f)] public uint thickness = 2; // Crosshair thickness
     [Range(0f, 25f)] public uint gap = 6; // Crosshair gap
  
     public Color32 crosshairColor; // Crosshair color
     public uint customColor;
     public byte redColorValue;
     public byte greenColorValue;
     public byte blueColorValue;
     public byte alphaColorValue;
  
     public bool dot = false; // Crosshair dot
     public bool refresh = false;
  
     private void Start()
     {
         Refresh();
     }
  
     private void Update()
     {
         //if (refresh)
         //{
         //    Refresh();
         //    refresh = false;
         //}
  
         Refresh();
     }
  
     private void CheckThickness()
     {
         if (thickness % 2 == 0 && gap % 2 == 1) // Thickness even, gap odd
         {
             gap--;
         }
  
         else if (thickness % 2 == 1 && gap % 2 == 0) // Thickness odd, gap even
         {
             gap++;
         }
  
         if (thickness > gap) // Fix crashing when pixels are overwriting
         {
             gap = thickness;
         }
     }
  
     private void CheckColor()
     {
         if (customColor == 0)
         {
             crosshairColor = Color.red;
         }
         else if (customColor == 1)
         {
             crosshairColor = Color.green;
         }
         else if (customColor == 2)
         {
             crosshairColor = Color.yellow;
         }
         else if (customColor == 3)
         {
             crosshairColor = Color.blue;
         }
         else if (customColor == 4)
         {
             crosshairColor = Color.cyan;
         }
         else if (customColor == 5)
         {
             crosshairColor = new Color32(redColorValue, greenColorValue, blueColorValue, alphaColorValue);
         }
     }
  
     private void Refresh()
     {
         // Fix thickness and gap diff
         CheckThickness();
         CheckColor();
  
         uint s, g, t;
  
         s = size;
         g = gap;
         t = thickness;
  
         string crosshair = "";
         g += t;
         float se = (2 * s + g) / 2f;
         uint sC = 2 * s + (g - t);
  
         crosshairTex = new Texture2D((int)sC, (int)sC, TextureFormat.RGBA32, false);
  
         for (int k = 0; k < s; k++)
         {
             for (int i = 0; i < se - t; i++)
                 crosshair += "0";
  
             for (int i = 0; i < t; i++)
                 crosshair += "Y";
  
             crosshair += "\n";
         }
  
         for (int i = 0; i < (g - 2 * t) / 2; i++)
             crosshair += "\n";
  
         for (int k = 0; k < t; k++)
         {
             for (int i = 0; i < s; i++)
                 crosshair += "X";
  
             for (int i = 0; i < g - t; i++)
                 crosshair += "0";
  
             for (int i = 0; i < s; i++)
                 crosshair += "X";
  
             crosshair += "\n";
         }
  
         for (int i = 0; i < (g - 2 * t) / 2; i++)
             crosshair += "\n";
  
         for (int k = 0; k < s; k++)
         {
             for (int i = 0; i < se - t; i++)
                 crosshair += "0";
  
             for (int i = 0; i < t; i++)
                 crosshair += "Y";
  
             if (k != s - 1)
                 crosshair += "\n";
         }
  
         for (int ys = 0; ys < crosshairTex.height; ys++)
             for (int xs = 0; xs < crosshairTex.width; xs++)
                 crosshairTex.SetPixel(xs, ys, new Color(0, 0, 0, 0));
  
         int x = 0; int y = 0;
         var crosshairARR = crosshair.Split(new char[] { '\n' }, System.StringSplitOptions.None);
         foreach (string cStr in crosshairARR)
         {
             foreach (char cChar in cStr)
             {
                 if (cChar == 'Y' || cChar == 'X')
                     crosshairTex.SetPixel(x, y, crosshairColor);
                 else
                     crosshairTex.SetPixel(x, y, new Color(0, 0, 0, 0));
                 x++;
             }
             x = 0;
             y++;
         }
  
         if (dot == true)
         {
             for (int yk = 0; yk < thickness; yk++)
                 for (int xk = 0; xk < thickness; xk++)
                 {
                     crosshairTex.SetPixel((int)(crosshairTex.width / 2 - (thickness / 2) + xk), (int)(crosshairTex.width / 2 - (thickness / 2) + yk), crosshairColor);
                 }
         }
  
         crosshairTex.Apply();
     }
  
     void OnGUI()
     {
         // Crosshair
         GUI.DrawTexture(new Rect(Screen.width / 2f - crosshairTex.width / 2f, Screen.height / 2f - crosshairTex.height / 2f, crosshairTex.width, crosshairTex.height), crosshairTex);
     }
 }
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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

147 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

Related Questions

Assigning UV Map to model at runtime 0 Answers

Get texture with UV Mesh Mapping 0 Answers

how to promote 3D environments for mobile apps with decent graphics 0 Answers

How to add decals to terrain texture? 1 Answer

Alpha Importing Completely White 2 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