2016-04-28 5 views
-1

Ich habe keine direkte Funktion im Abschnitt Farbe gefunden, oder es gibt keine direkte C# Unity-Funktion zum Auswählen der Farbe nach dem Rendern. Wie ist der beste Ansatz für die Auswahl der Farbe in der Mausposition?
Ich habe Nachforschungen gemacht und es sieht so aus, als ob es möglich ist, einen Screenshot zu machen und dann in die Textur zu schauen, die die Mausposition berechnet.Wie wird die Farbe am Bildschirm in der Mausposition mit Unity und C# erfasst?

Input.GetMouseButtonDown(0) 

Application.CaptureScreenshot("Screenshot.png"); 

// get the color pixel in the same coordinates of the mouse position 
Vector3 mouseCoordinates = Input.mousePosition; 
myFinalColor = tex.GetPixel((int)mouseCoordinates.x, (int)mouseCoordinates.y); 

Oder muss ich eine zweite Kamera machen und sie an einen Mesh-Renderer anhängen?

+0

Sie müssen wirklich anfangen, Antworten zu akzeptieren. Wenn Sie sie nicht hilfreich finden, können Sie sie gut kommentieren und ihnen sagen, dass ihre Antwort nicht funktioniert, anstatt sie zu ignorieren. – Programmer

+0

einige der Forschung erfordern, bevor Sie die Frage über Stackoverflow stellen. –

+0

Ja Programm Danke. So bald wie möglich. Geben Sie genügend Zeit, um die Daten aufzuwecken und zu analysieren, und bestätigen Sie, dass der Code wirklich gut funktioniert, um richtige Antworten auszuwählen. Ja Mohammad Faizan Khan Ich habe eine Woche lang in die Dokumentation geschaut. Und ich habe andere ähnliche Fragen mit negativen Punkten hier in stackoverflow. Ich habe keine Idee, wie ich das Problem angehen soll. Danke, dass du darauf hingewiesen hast. –

Antwort

1

Sie müssen nur GetPixel(x,y)

Dies ist sehr einfach zu bedienen.

  • Speichern Screenshot Texture2D zum Beispiel MyTexture
  • Und fügen .GetPixel (x Postion von moue, Y-Position der Maus)
  • speichern Sie es auf Farbe für GetScreenShot (Ihre Ansicht zu Texture2D machen)

    Color TheColorPicked; 
        if (Input.GetMouseButtonDown(0)) 
        { 
         TheColorPicked = MyTexture.GetPixel(Input.mousePosition.x, 
                   Input.mousePosition.y);  
        } 
    
1

Ja können Sie Ihre eigene Farbauswahl machen. Hier ist der Code dank Git-Hub page.

using UnityEngine; 
using System.Collections; 

// relies on: http://forum.unity3d.com/threads/12031-create-random-colors?p=84625&viewfull=1#post84625 

public class ColorPicker : MonoBehaviour { 

    public bool useDefinedPosition = false; 
    public int positionLeft = 0; 
    public int positionTop = 0; 

    // the solid texture which everything is compared against 
    public Texture2D colorPicker; 

    // the picker being displayed 
    private Texture2D displayPicker; 

    // the color that has been chosen 
    public Color setColor; 
    private Color lastSetColor; 

    public bool useDefinedSize = false; 
    public int textureWidth = 360; 
    public int textureHeight = 120; 

    private float saturationSlider = 0.0F; 
    private Texture2D saturationTexture; 

    private Texture2D styleTexture; 

    public bool showPicker = false; 

    void Awake() { 
     if (!useDefinedPosition) { 
     positionLeft = (Screen.width/2) - (textureWidth/2); 
     positionTop = (Screen.height/2) - (textureHeight/2); 
     } 

     // if a default color picker texture hasn't been assigned, make one dynamically 
     if (!colorPicker) { 
     colorPicker = new Texture2D(textureWidth, textureHeight, TextureFormat.ARGB32, false); 
     ColorHSV hsvColor; 
     for (int i = 0; i < textureWidth; i++) { 
     for (int j = 0; j < textureHeight; j++) { 
      hsvColor = new ColorHSV((float)i, (1.0f/j) * textureHeight, 1.0f); 
      colorPicker.SetPixel(i, j, hsvColor.ToColor()); 
     } 
     } 
     } 
     colorPicker.Apply(); 
     displayPicker = colorPicker; 

     if (!useDefinedSize) { 
     textureWidth = colorPicker.width; 
     textureHeight = colorPicker.height; 
     } 

     float v = 0.0F; 
     float diff = 1.0f/textureHeight; 
     saturationTexture = new Texture2D(20, textureHeight); 
     for (int i = 0; i < saturationTexture.width; i++) { 
     for (int j = 0; j < saturationTexture.height; j++) { 
     saturationTexture.SetPixel(i, j, new Color(v, v, v)); 
     v += diff; 
     } 
     v = 0.0F; 
     } 
     saturationTexture.Apply(); 

     // small color picker box texture 
     styleTexture = new Texture2D(1, 1); 
     styleTexture.SetPixel(0, 0, setColor); 
    } 

    void OnGUI(){ 
     if (!showPicker) return; 

     GUI.Box(new Rect(positionLeft - 3, positionTop - 3, textureWidth + 60, textureHeight + 60), ""); 

     if (GUI.RepeatButton(new Rect(positionLeft, positionTop, textureWidth, textureHeight), displayPicker)) { 
     int a = (int)Input.mousePosition.x; 
     int b = Screen.height - (int)Input.mousePosition.y; 

     setColor = displayPicker.GetPixel(a - positionLeft, -(b - positionTop)); 
     lastSetColor = setColor; 
     } 

     saturationSlider = GUI.VerticalSlider(new Rect(positionLeft + textureWidth + 3, positionTop, 10, textureHeight), saturationSlider, 1, -1); 
    setColor = lastSetColor + new Color(saturationSlider, saturationSlider, saturationSlider); 
     GUI.Box(new Rect(positionLeft + textureWidth + 20, positionTop, 20, textureHeight), saturationTexture); 

     if (GUI.Button(new Rect(positionLeft + textureWidth - 60, positionTop + textureHeight + 10, 60, 25), "Apply")) { 
      setColor = styleTexture.GetPixel(0, 0); 

     // hide picker 
     showPicker = false; 
     } 

     // color display 
     GUIStyle style = new GUIStyle(); 
     styleTexture.SetPixel(0, 0, setColor); 
     styleTexture.Apply(); 

     style.normal.background = styleTexture; 
     GUI.Box(new Rect(positionLeft + textureWidth + 10, positionTop + textureHeight + 10, 30, 30), new GUIContent(""), style); 
    } 

} 

Sie können auch diese hilfreich Asset Store-Pakete 12 finden.

+0

Thx Mohammad Ich probiere Package (1) vorher aber hat CameraBgColor depreciate script und so springe ich es. Und (2) hat Unix-Zeilenende und weiß nicht, warum in Mono verknallt. Es gibt auch andere, aber wählen Sie nicht die Farbe vom Bildschirm. –

+0

Für diese unvollständige Lösung wird eine zweite Klasse benötigt: "ColorHSV" Wonderful Script, das ist wahrscheinlich dieses von MatthewW und Übersetzung in C# von Karsnen_2: http://forum.unity3d.com/threads/create-random-colors.12031/ # post-84625 –

+0

Aktualisieren Sie die Antwort für zukünftige Verwendung! Danke –

Verwandte Themen