Stop ReSharper formatting C# field attributes - c#

In Unity, attributes are often used to format the inspector window for a component. To make things look tidy in the code, I have my code look like this:
[Header("Items")]
[SerializeField] private NamedAction[] items;
[Space]
[Header("References")]
[SerializeField] private Button button;
[SerializeField] private RectTransform template;
[SerializeField] private RectTransform templateItem;
However, ReSharper wants to format this to look like this:
[Header("Items")] [SerializeField] private NamedAction[] items;
[Space] [Header("References")] [SerializeField] private Button button;
[SerializeField] private RectTransform template;
[SerializeField] private RectTransform templateItem;
This hurts readability, especially in more complex components. I'm wondering which option can be used to disable this behaviour.
There is an option "Line Breaks and Wrapping -> Other -> Place field attribute on same line", but this will still mess with my attributes formatting. I would like to tell ReSharper to never touch my field attributes.

Related

Unity 2D : Get pixel of color of Raw Image

I want to know how I can get the pixel color of my rawImage texture.
This is my scene:
public class ColorPicker : MonoBehaviour, IPointerClickHandler
{
public GameObject Cube;
public void OnPointerClick(PointerEventData eventData)
{
if (Input.GetMouseButtonUp(1))
{
Vector3 localPosition = transform.InverseTransformPoint(eventData.pressPosition);
Color color = (GetComponent<RawImage>().texture as Texture2D).GetPixel(Convert.ToInt32(localPosition.x),
Convert.ToInt32(localPosition.y));
Cube.GetComponent<Renderer>().material.color = color;
}
}
}
And as you can see, with this code, it work but it is not scalled.
In fact, I can't reach the blue color. I only can get the top of my square.
Can you help me how to get the color when I press the right click ?
Thanks
The mouse position you get using
Input.mousePosition
is in screen space (meaning the position you get is relative to the screen size).
What you want to get is the mouse position in canvas space (relative to the canvas size, so you don't have to worry about the canvas size).
You can do this by implementing IPointerClickHandler in your ColorPicker script (which is also better practise than using Input.mousePosition).
using UnityEngine.EventSystems;
public class ColorPicker : MonoBehaviour, IPointerClickHandler{
public GameObject Cube;
void OnPointerClick(PointerEventData eventData)
{
//eventData.position
//Vector3 localPosition = transform.InverseTransformPoint(eventData.pressPosition);
//ignore z coordinate
}
}
The PointerEventData contains a lot of useful information, you can get more information in the unity documentation https://docs.unity3d.com/ScriptReference/EventSystems.PointerEventData.html
You probably still need to convert your mouse position using
transform.InverseTransformPoint(eventData.pressPosition);
as i have not tested this yet. But i hope this points you in the right direction!

Unity - C# - attach GameObject to public Button CommunicationButton;

I cannot solve this problem, I hope someone knows how to do this.
What I have
A button and a text field on a canvas, see image:
I have a script called HumanInstructions, that initializes like this:
public Text CommunicationMessage;
public Button CommunicationButton;
Right now I can assign the corresponding GameObject in the inspector, by choosing the button/textfield in a dropdown menu.
What I want, is choosing the GameObject through script. The textfield is called CrazyMsg and the button is called CommunicationButton. So I want something like this:
public override void OnAwake ()
{
CommunicationMessage = GameObject.find("CrazyMsg");
CommunicationButton = GameObject.find("CommunicationButton");
}
It doesn't work. Help!
It is GameObject.Find not GameObject.find. You also need to get Component after finding the GameObject.
public Text CommunicationMessage;
public Button CommunicationButton;
void Start()
{
CommunicationMessage = GameObject.Find("CrazyMsg").GetComponent<Text>();
CommunicationButton = GameObject.Find("CommunicationButton").GetComponent<Button>();
//Register Button Click
CommunicationButton.onClick.AddListener(() => buttonClickCallBack());
}
//Will be called when CommunicationButton Button is clicked!
void buttonClickCallBack()
{
CommunicationMessage.text = ....
}
You can also regsiter Button with CommunicationButton.onClick.AddListener(delegate { buttonClickCallBack(); }); or CommunicationButton.onClick.AddListener(buttonClickCallBack);

Drag a sprite by its 2D Polygon Collider component

I've got a draggable class that works great for dragging entire sprites and I'm trying to modify it to register dragging only within the defined 2D polygon collider component of the sprite object. I'm having a really hard time getting the drag to only register when I click within the boundaries of the polygon collider. Right now it's still dragging wherever I click-drag on the sprite including outside the boundaries of my polygon collider. I could really use some guidance on this. I've also attached an image to illustrate what I'm trying to achieve. Thanks!
PolygonCollider2D collider;
void Start ()
{
collider = transform.GetComponent<PolygonCollider2D>();
}
public void OnDrag (PointerEventData eventData)
{
Vector3 mousePosition = new Vector3(eventData.position.x, eventData.position.y, 0);
collider.transform.position = mousePosition;
if(transform.parent.gameObject == partsPanel)
transform.SetParent(dragLayer.transform);
if(transform.parent.gameObject == buildBoard)
transform.SetParent(dragLayer.transform);
}
AFAIK, The polygon collider we added to the sprite is editable. Goto inspector, collider component and there is a button which allow editing collider area. Adjust the points of this collider to cover just the desired area. This way only the desired area will receive touch events.
Ok, I know this is very old thread, but I had similar problem and I found a solution so I will post it here :)
Just create a class with this code and put it on your object
using UnityEngine;
[RequireComponent(typeof(RectTransform), typeof(Collider2D))]
public class Collider2DRaycastFilter : MonoBehaviour, ICanvasRaycastFilter
{
Collider2D myCollider;
RectTransform rectTransform;
void Awake()
{
myCollider = GetComponent<Collider2D>();
rectTransform = GetComponent<RectTransform>();
}
public bool IsRaycastLocationValid(Vector2 screenPos, Camera eventCamera)
{
var worldPoint = Vector3.zero;
var isInside = RectTransformUtility.ScreenPointToWorldPointInRectangle(
rectTransform,
screenPos,
eventCamera,
out worldPoint
);
if (isInside)
isInside = myCollider.OverlapPoint(worldPoint);
return isInside;
}
}

Unity OnMouseEnter not working

I'll post the link to the project here I seem to be unable to figure out how to make my OnMouseEnter function work (it has worked in a previous project but won't now).
Google Drive - Space Cadets
using UnityEngine;
using System.Collections;
public class MouseHover : MonoBehaviour {
void Start(){
GetComponent<Renderer>().material.color = Color.black;
}
void OnMouseEnter(){
GetComponent<Renderer>().material.color = Color.white;
}
void OnMouseExit() {
GetComponent<Renderer>().material.color = Color.black;
}
}
this code is attached to 5 menu items on the main menu which is supposed to make the text color go from black to white when the mouse hovers over it and I know that the first part of the script works but the OnMouseEnter is the part where it seems to not recognize (my thought is that the box collider that is supposed to recognize the mouse event isn't recognizing it).
The Background gameobject in the scene overlaps the menu items. Change the position of the Background gameobject to be slightly behind the menu items and OnMouseEnter will register. Also, the line of code that changes the color on the material inside OnMouseEnter doesn't actually change the color of the text because the Font Material Text Color is set to black, when it should to be set to white while the TextMesh Color attribute is the one that should be toggled like so:
void Start()
{
GetComponent<TextMesh>().color = Color.black;
}
void OnMouseEnter()
{
GetComponent<TextMesh>().color = Color.white;
}
void OnMouseExit()
{
GetComponent<TextMesh>().color = Color.black;
}
That's how I got it to work.

How can I show a vertical line on mouse over?

I'm trying to show a vertical line in position X of the cursor. I'm trying to do that in a slider control, but I couldn't made it. This will be showed while the user has its cursor on it.
I only know that I need to modify the template. Is so much difficult to do that? If not, can you help me?
Thanks.
This is not easy to attain using templating because of the fact that mouse position is not a dependency property and mouse move is not a routed event. This really comes down to what you want to do, if it's to just show a vertical line whether the mouse is then I agree with Dany to use adorners, as you're not really interested in the slider itself. However, if it's to move the thumb to where the mouse is I would use an attached property.
The reason I use attached properties rather than hooking up the events directly on the control is that it provides more modularised and reusable codebase and makes it more obvious of the visual effect in the XAML, rather than needing to look at C# code as well.
Here's what I would suggest
public class SliderHelperPackage
{
public static readonly DependencyProperty BindThumbToMouseProperty = DependencyProperty.RegisterAttached(
"BindThumbToMouse", typeof(bool), typeof(SliderHelperPackage), new PropertyMetadata(false, OnBindThumbToMouseChanged));
public static void SetBindThumbToMouse(UIElement element, bool value)
{
element.SetValue(BindThumbToMouseProperty, value);
}
public static bool GetBindThumbToMouse(UIElement element)
{
return (bool)element.GetValue(BindThumbToMouseProperty);
}
private static void OnBindThumbToMouseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue.Equals(e.OldValue))
return;
var slider = d as Slider;
if (slider == null)
throw new ArgumentException(#"dependency object must be a slider", "d");
if ((bool) e.NewValue)
{
slider.MouseMove += SliderMouseMove;
}
else
{
slider.MouseMove -= SliderMouseMove;
}
}
static void SliderMouseMove(object sender, MouseEventArgs e)
{
var slider = (Slider) sender;
var position = e.GetPosition(slider);
// When the mouse moves, we update the slider's value so it's where the ticker is (we take into account margin's on the track)
slider.Value = (slider.Maximum - slider.Minimum)*Math.Max(position.X-5,0)/(slider.ActualWidth-10) + slider.Minimum;
}
}
And you can then use it in your XAML like so. So when you set this to true we hook up to the mouse move event on the slider and change its value so the thumb follows the mouse
<Slider SliderPosn:SliderHelperPackage.BindThumbToMouse="True" Margin="5" Height="25" VerticalAlignment="Top"/>
http://www.codeproject.com/KB/WPF/adornedcontrol.aspx
this reference should give you all necessary information to fix your issue
cheers!

Resources