Scale ingame color picker
Hey,
I am working on a 3D room editor where you can grab an object from a menu and put it back in a room. You also get the option to give these objects a different color, this is done by the color picker. I now have a script that works the way I want it only if I enlarge the color picker, then he does not pick up the colors anymore and he does not move the selector circle anymore.
How do I solve this?
GIF Color picker at 1.8 scale
GIF Color picker at above 2 scale
Color picker script:
Color Data;
SpriteRenderer SpriteRenderer;
GameObject ColorPicker;
GameObject Selector;
BoxCollider Collider;
public GameObject target;
Ray rayray;
private Plane MyPlane;
public int Width { get { return SpriteRenderer.sprite.texture.width; } }
public int Height { get { return SpriteRenderer.sprite.texture.height; } }
public Color Color;
void Awake()
{
ColorPicker = transform.Find("ColorPicker").gameObject;
SpriteRenderer = ColorPicker.GetComponent<SpriteRenderer>();
Selector = transform.Find("Selector").gameObject;
Collider = ColorPicker.GetComponent<BoxCollider>();
Data = SpriteRenderer.sprite.texture.GetPixels();
Color = Color.white;
Debug.Log(Collider);
MyPlane = new Plane(transform.TransformDirection(Vector3.forward), transform.position);
}
void Update()
{
if (Input.GetMouseButton(0))
{
rayray = Camera.main.ScreenPointToRay(Input.mousePosition);
MyPlane = new Plane(transform.TransformDirection(Vector3.forward), transform.position);
Vector3 screenPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
screenPos = new Vector3(screenPos.x, screenPos.y);
//check if we clicked this picker control
RaycastHit ray = Physics.RaycastAll(rayray.origin, rayray.direction);
foreach (RaycastHit h in ray)
{
Debug.Log(h.collider.name);
if (h.collider.name == "ColorPicker")
{
Selector.transform.position = screenPos;
//get color data
screenPos -= ColorPicker.transform.position;
int x = (int)(screenPos.x * Width);
int y = (int)(screenPos.y * Height) + Height;
if (x > 0 && x < Width && y > 0 && y < Height)
{
Color = Data[y * Width + x];
target.GetComponent<Renderer>().material.color = Color;
Debug.Log(Width);
Debug.Log(Height);
}
}
}
}
}
EDIT:
GIF
This is the inspector from the color picker color field
This is the inspector from the main camera
IMAGE:
This is the inspector from the color picker color field
This is the inspector from the main camera
c# unity3d scale color-picker
add a comment |
Hey,
I am working on a 3D room editor where you can grab an object from a menu and put it back in a room. You also get the option to give these objects a different color, this is done by the color picker. I now have a script that works the way I want it only if I enlarge the color picker, then he does not pick up the colors anymore and he does not move the selector circle anymore.
How do I solve this?
GIF Color picker at 1.8 scale
GIF Color picker at above 2 scale
Color picker script:
Color Data;
SpriteRenderer SpriteRenderer;
GameObject ColorPicker;
GameObject Selector;
BoxCollider Collider;
public GameObject target;
Ray rayray;
private Plane MyPlane;
public int Width { get { return SpriteRenderer.sprite.texture.width; } }
public int Height { get { return SpriteRenderer.sprite.texture.height; } }
public Color Color;
void Awake()
{
ColorPicker = transform.Find("ColorPicker").gameObject;
SpriteRenderer = ColorPicker.GetComponent<SpriteRenderer>();
Selector = transform.Find("Selector").gameObject;
Collider = ColorPicker.GetComponent<BoxCollider>();
Data = SpriteRenderer.sprite.texture.GetPixels();
Color = Color.white;
Debug.Log(Collider);
MyPlane = new Plane(transform.TransformDirection(Vector3.forward), transform.position);
}
void Update()
{
if (Input.GetMouseButton(0))
{
rayray = Camera.main.ScreenPointToRay(Input.mousePosition);
MyPlane = new Plane(transform.TransformDirection(Vector3.forward), transform.position);
Vector3 screenPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
screenPos = new Vector3(screenPos.x, screenPos.y);
//check if we clicked this picker control
RaycastHit ray = Physics.RaycastAll(rayray.origin, rayray.direction);
foreach (RaycastHit h in ray)
{
Debug.Log(h.collider.name);
if (h.collider.name == "ColorPicker")
{
Selector.transform.position = screenPos;
//get color data
screenPos -= ColorPicker.transform.position;
int x = (int)(screenPos.x * Width);
int y = (int)(screenPos.y * Height) + Height;
if (x > 0 && x < Width && y > 0 && y < Height)
{
Color = Data[y * Width + x];
target.GetComponent<Renderer>().material.color = Color;
Debug.Log(Width);
Debug.Log(Height);
}
}
}
}
}
EDIT:
GIF
This is the inspector from the color picker color field
This is the inspector from the main camera
IMAGE:
This is the inspector from the color picker color field
This is the inspector from the main camera
c# unity3d scale color-picker
add a comment |
Hey,
I am working on a 3D room editor where you can grab an object from a menu and put it back in a room. You also get the option to give these objects a different color, this is done by the color picker. I now have a script that works the way I want it only if I enlarge the color picker, then he does not pick up the colors anymore and he does not move the selector circle anymore.
How do I solve this?
GIF Color picker at 1.8 scale
GIF Color picker at above 2 scale
Color picker script:
Color Data;
SpriteRenderer SpriteRenderer;
GameObject ColorPicker;
GameObject Selector;
BoxCollider Collider;
public GameObject target;
Ray rayray;
private Plane MyPlane;
public int Width { get { return SpriteRenderer.sprite.texture.width; } }
public int Height { get { return SpriteRenderer.sprite.texture.height; } }
public Color Color;
void Awake()
{
ColorPicker = transform.Find("ColorPicker").gameObject;
SpriteRenderer = ColorPicker.GetComponent<SpriteRenderer>();
Selector = transform.Find("Selector").gameObject;
Collider = ColorPicker.GetComponent<BoxCollider>();
Data = SpriteRenderer.sprite.texture.GetPixels();
Color = Color.white;
Debug.Log(Collider);
MyPlane = new Plane(transform.TransformDirection(Vector3.forward), transform.position);
}
void Update()
{
if (Input.GetMouseButton(0))
{
rayray = Camera.main.ScreenPointToRay(Input.mousePosition);
MyPlane = new Plane(transform.TransformDirection(Vector3.forward), transform.position);
Vector3 screenPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
screenPos = new Vector3(screenPos.x, screenPos.y);
//check if we clicked this picker control
RaycastHit ray = Physics.RaycastAll(rayray.origin, rayray.direction);
foreach (RaycastHit h in ray)
{
Debug.Log(h.collider.name);
if (h.collider.name == "ColorPicker")
{
Selector.transform.position = screenPos;
//get color data
screenPos -= ColorPicker.transform.position;
int x = (int)(screenPos.x * Width);
int y = (int)(screenPos.y * Height) + Height;
if (x > 0 && x < Width && y > 0 && y < Height)
{
Color = Data[y * Width + x];
target.GetComponent<Renderer>().material.color = Color;
Debug.Log(Width);
Debug.Log(Height);
}
}
}
}
}
EDIT:
GIF
This is the inspector from the color picker color field
This is the inspector from the main camera
IMAGE:
This is the inspector from the color picker color field
This is the inspector from the main camera
c# unity3d scale color-picker
Hey,
I am working on a 3D room editor where you can grab an object from a menu and put it back in a room. You also get the option to give these objects a different color, this is done by the color picker. I now have a script that works the way I want it only if I enlarge the color picker, then he does not pick up the colors anymore and he does not move the selector circle anymore.
How do I solve this?
GIF Color picker at 1.8 scale
GIF Color picker at above 2 scale
Color picker script:
Color Data;
SpriteRenderer SpriteRenderer;
GameObject ColorPicker;
GameObject Selector;
BoxCollider Collider;
public GameObject target;
Ray rayray;
private Plane MyPlane;
public int Width { get { return SpriteRenderer.sprite.texture.width; } }
public int Height { get { return SpriteRenderer.sprite.texture.height; } }
public Color Color;
void Awake()
{
ColorPicker = transform.Find("ColorPicker").gameObject;
SpriteRenderer = ColorPicker.GetComponent<SpriteRenderer>();
Selector = transform.Find("Selector").gameObject;
Collider = ColorPicker.GetComponent<BoxCollider>();
Data = SpriteRenderer.sprite.texture.GetPixels();
Color = Color.white;
Debug.Log(Collider);
MyPlane = new Plane(transform.TransformDirection(Vector3.forward), transform.position);
}
void Update()
{
if (Input.GetMouseButton(0))
{
rayray = Camera.main.ScreenPointToRay(Input.mousePosition);
MyPlane = new Plane(transform.TransformDirection(Vector3.forward), transform.position);
Vector3 screenPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
screenPos = new Vector3(screenPos.x, screenPos.y);
//check if we clicked this picker control
RaycastHit ray = Physics.RaycastAll(rayray.origin, rayray.direction);
foreach (RaycastHit h in ray)
{
Debug.Log(h.collider.name);
if (h.collider.name == "ColorPicker")
{
Selector.transform.position = screenPos;
//get color data
screenPos -= ColorPicker.transform.position;
int x = (int)(screenPos.x * Width);
int y = (int)(screenPos.y * Height) + Height;
if (x > 0 && x < Width && y > 0 && y < Height)
{
Color = Data[y * Width + x];
target.GetComponent<Renderer>().material.color = Color;
Debug.Log(Width);
Debug.Log(Height);
}
}
}
}
}
EDIT:
GIF
This is the inspector from the color picker color field
This is the inspector from the main camera
IMAGE:
This is the inspector from the color picker color field
This is the inspector from the main camera
c# unity3d scale color-picker
c# unity3d scale color-picker
edited Nov 22 '18 at 10:09
craftercis
asked Nov 21 '18 at 16:27
crafterciscraftercis
12
12
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
It is hard to know for sure without seeing your scene in the Unity editor but I have a few things I would check that may solve your problem.
Check to make sure that when you are scaling your GameObject that the BoxCollider is being scaled correctly. You should be able to see the Gizmo when selecting your GameObject after you scale it. Make sure it is covering the same areas of the UI before/after you scale it. The BoxCollider is used to detect mouse clicks on the object as a whole and unless the ray hits that collider, none of the other functionality will work.
It doesn't look like the script is taking into account scale changes for the color picker. Take a look where you get your X/Y coordinates for picking the color (under the //get color data comment). You will notice it multiplies the screen position by Height and Width which are taken from the size of your texture. This would need to be scaled accordingly to sample the correct area of the texture.
If you still can't get it to work, I would suggest posting a screen shot of your scene hierarchy and relevant GameObjects so we can see how it is all setup.
Hey Shawn, Look at my edit post I have put the inspector from the color field and the main-camera in it. I can now enlarge it because I have set the clipping planes (near) to 0. Only if I now want a color near the edge does my block become black or gray Do you know how I solve this?
– craftercis
Nov 22 '18 at 10:05
Do you know how I can solve this?
– craftercis
Nov 22 '18 at 21:59
I didn't see anything obvious in your pictures. Any chance you could upload the project somewhere and send me a link? Would be happy to do some testing on my side. Thanks!
– Shawn Lehner
Nov 26 '18 at 3:15
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416488%2fscale-ingame-color-picker%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It is hard to know for sure without seeing your scene in the Unity editor but I have a few things I would check that may solve your problem.
Check to make sure that when you are scaling your GameObject that the BoxCollider is being scaled correctly. You should be able to see the Gizmo when selecting your GameObject after you scale it. Make sure it is covering the same areas of the UI before/after you scale it. The BoxCollider is used to detect mouse clicks on the object as a whole and unless the ray hits that collider, none of the other functionality will work.
It doesn't look like the script is taking into account scale changes for the color picker. Take a look where you get your X/Y coordinates for picking the color (under the //get color data comment). You will notice it multiplies the screen position by Height and Width which are taken from the size of your texture. This would need to be scaled accordingly to sample the correct area of the texture.
If you still can't get it to work, I would suggest posting a screen shot of your scene hierarchy and relevant GameObjects so we can see how it is all setup.
Hey Shawn, Look at my edit post I have put the inspector from the color field and the main-camera in it. I can now enlarge it because I have set the clipping planes (near) to 0. Only if I now want a color near the edge does my block become black or gray Do you know how I solve this?
– craftercis
Nov 22 '18 at 10:05
Do you know how I can solve this?
– craftercis
Nov 22 '18 at 21:59
I didn't see anything obvious in your pictures. Any chance you could upload the project somewhere and send me a link? Would be happy to do some testing on my side. Thanks!
– Shawn Lehner
Nov 26 '18 at 3:15
add a comment |
It is hard to know for sure without seeing your scene in the Unity editor but I have a few things I would check that may solve your problem.
Check to make sure that when you are scaling your GameObject that the BoxCollider is being scaled correctly. You should be able to see the Gizmo when selecting your GameObject after you scale it. Make sure it is covering the same areas of the UI before/after you scale it. The BoxCollider is used to detect mouse clicks on the object as a whole and unless the ray hits that collider, none of the other functionality will work.
It doesn't look like the script is taking into account scale changes for the color picker. Take a look where you get your X/Y coordinates for picking the color (under the //get color data comment). You will notice it multiplies the screen position by Height and Width which are taken from the size of your texture. This would need to be scaled accordingly to sample the correct area of the texture.
If you still can't get it to work, I would suggest posting a screen shot of your scene hierarchy and relevant GameObjects so we can see how it is all setup.
Hey Shawn, Look at my edit post I have put the inspector from the color field and the main-camera in it. I can now enlarge it because I have set the clipping planes (near) to 0. Only if I now want a color near the edge does my block become black or gray Do you know how I solve this?
– craftercis
Nov 22 '18 at 10:05
Do you know how I can solve this?
– craftercis
Nov 22 '18 at 21:59
I didn't see anything obvious in your pictures. Any chance you could upload the project somewhere and send me a link? Would be happy to do some testing on my side. Thanks!
– Shawn Lehner
Nov 26 '18 at 3:15
add a comment |
It is hard to know for sure without seeing your scene in the Unity editor but I have a few things I would check that may solve your problem.
Check to make sure that when you are scaling your GameObject that the BoxCollider is being scaled correctly. You should be able to see the Gizmo when selecting your GameObject after you scale it. Make sure it is covering the same areas of the UI before/after you scale it. The BoxCollider is used to detect mouse clicks on the object as a whole and unless the ray hits that collider, none of the other functionality will work.
It doesn't look like the script is taking into account scale changes for the color picker. Take a look where you get your X/Y coordinates for picking the color (under the //get color data comment). You will notice it multiplies the screen position by Height and Width which are taken from the size of your texture. This would need to be scaled accordingly to sample the correct area of the texture.
If you still can't get it to work, I would suggest posting a screen shot of your scene hierarchy and relevant GameObjects so we can see how it is all setup.
It is hard to know for sure without seeing your scene in the Unity editor but I have a few things I would check that may solve your problem.
Check to make sure that when you are scaling your GameObject that the BoxCollider is being scaled correctly. You should be able to see the Gizmo when selecting your GameObject after you scale it. Make sure it is covering the same areas of the UI before/after you scale it. The BoxCollider is used to detect mouse clicks on the object as a whole and unless the ray hits that collider, none of the other functionality will work.
It doesn't look like the script is taking into account scale changes for the color picker. Take a look where you get your X/Y coordinates for picking the color (under the //get color data comment). You will notice it multiplies the screen position by Height and Width which are taken from the size of your texture. This would need to be scaled accordingly to sample the correct area of the texture.
If you still can't get it to work, I would suggest posting a screen shot of your scene hierarchy and relevant GameObjects so we can see how it is all setup.
answered Nov 21 '18 at 16:56
Shawn LehnerShawn Lehner
1,260712
1,260712
Hey Shawn, Look at my edit post I have put the inspector from the color field and the main-camera in it. I can now enlarge it because I have set the clipping planes (near) to 0. Only if I now want a color near the edge does my block become black or gray Do you know how I solve this?
– craftercis
Nov 22 '18 at 10:05
Do you know how I can solve this?
– craftercis
Nov 22 '18 at 21:59
I didn't see anything obvious in your pictures. Any chance you could upload the project somewhere and send me a link? Would be happy to do some testing on my side. Thanks!
– Shawn Lehner
Nov 26 '18 at 3:15
add a comment |
Hey Shawn, Look at my edit post I have put the inspector from the color field and the main-camera in it. I can now enlarge it because I have set the clipping planes (near) to 0. Only if I now want a color near the edge does my block become black or gray Do you know how I solve this?
– craftercis
Nov 22 '18 at 10:05
Do you know how I can solve this?
– craftercis
Nov 22 '18 at 21:59
I didn't see anything obvious in your pictures. Any chance you could upload the project somewhere and send me a link? Would be happy to do some testing on my side. Thanks!
– Shawn Lehner
Nov 26 '18 at 3:15
Hey Shawn, Look at my edit post I have put the inspector from the color field and the main-camera in it. I can now enlarge it because I have set the clipping planes (near) to 0. Only if I now want a color near the edge does my block become black or gray Do you know how I solve this?
– craftercis
Nov 22 '18 at 10:05
Hey Shawn, Look at my edit post I have put the inspector from the color field and the main-camera in it. I can now enlarge it because I have set the clipping planes (near) to 0. Only if I now want a color near the edge does my block become black or gray Do you know how I solve this?
– craftercis
Nov 22 '18 at 10:05
Do you know how I can solve this?
– craftercis
Nov 22 '18 at 21:59
Do you know how I can solve this?
– craftercis
Nov 22 '18 at 21:59
I didn't see anything obvious in your pictures. Any chance you could upload the project somewhere and send me a link? Would be happy to do some testing on my side. Thanks!
– Shawn Lehner
Nov 26 '18 at 3:15
I didn't see anything obvious in your pictures. Any chance you could upload the project somewhere and send me a link? Would be happy to do some testing on my side. Thanks!
– Shawn Lehner
Nov 26 '18 at 3:15
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416488%2fscale-ingame-color-picker%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown