Unity 5 Prevent Scroll Rect From Moving With Mouse - unity3d

How do I prevent the Scroll Rect from moving with the mouse say for example I only want a scroll bar to move it and not by dragging across the image or text with the mouse?

SubClass ScrollRect and override it's drag handlers?
Untested but should work:
public class NoDragScrollRect : ScrollRect {
public override void OnBeginDrag(PointerEventData eventData) { }
public override void OnDrag(PointerEventData eventData) { }
public override void OnEndDrag(PointerEventData eventData) { }
}

A simpler solution would be to add a canvas group to the scrollable RectTransform and set it to not block raycast. That way no drag will happen on the RectTransform. This of course only works if you dont need the RectTransform to be interactable at all, otherwise the other answer will do the trick

Related

Infinite Vertical Scrolling in Unity3D

While I try to use scrolling background vertically, the patches appear while scrolling vertically.
Can you explain me why? If the background is scrolling horizontally it works fine!
Maybe due to lighting problems.
Scroll Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Scrollscript : MonoBehaviour
{
private float scrollSpeed = 0.2f;
private Renderer rend;
private Vector2 offset;
// Start is called before the first frame update
void Start() {
rend = GetComponent<Renderer>();
}
// Update is called once per frame
void Update() {
offset = new Vector2(0, Time.time * scrollSpeed);
rend.material.mainTextureOffset = offset;
}
}
You need to create the image so that it fits with its copy on horizontal and vertical.
i.e looping of an image should be created so that when the image scrolls for second time, it is not noticed. This is something you need to solve in photoshop or some other photo editing software rather than unity.

How to call a function from a prefab button?

I have a prefab button and a non prefab Game manager. I want this button to be cloned and call a function in Game Manager. How can I do that?
[SerializeField]
private GameObject buttonPrefab;
[SerializeField]
private Transform canvas;
public void CloneButton()
{
var buttonComponent = Instantiate(buttonPrefab, canvas).GetComponent<Button>();
buttonComponent.onClick.AddListener(TaskOnClick);
}
public void TaskOnClick()
{
// things happen when the button clicked
}
You have to give a reference for the prefab, and the canvas object as a parent for the button via inspector.

Gun should shoot only when button is touched instead of entire screen

I've made a 3d game and I have a first person controller, control by a joystick. I want my player to shoot only when I touch a button on the screen, but now my player shoots if I touch anything on the screen. This is the script for the shoot function:
using UnityEngine;
using UnityEngine.UI;
public class Gun : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public Camera fpsCam;
public Button button;
// Start is called before the first frame update
void Start()
{
//Calls the TaskOnClick/TaskWithParameters/ButtonClicked method when you click the Button
button.onClick.AddListener(TaskOnClick);
}
// Update is called once per frame
void Update()
{
Vector3 forward = transform.TransformDirection(Vector3.forward) * 10;
Debug.DrawRay(transform.position, forward, Color.green);
}
void Shoot()
{
RaycastHit hit;
if( Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if(target != null)
{
target.TakeDamage(damage);
}
}
}
void OnCollisionEnter(Collision collision)
{
Debug.DrawRay(collision.contacts[0].point, collision.contacts[0].normal, Color.green, 2, false);
}
void TaskOnClick()
{
Shoot();
}
}
Your Shoot() method runs whenever you press the "Fire1" button, since the only check you are doing is if (Input.GetButtonDown("Fire1")).
If you want to depend on a button to fire you need to take this part of the code out and call Shoot() whenever you click the button.
You can add the event to the button using code, as explained here
public Button m_YourFirstButton;
void Start()
{
//Calls the TaskOnClick/TaskWithParameters/ButtonClicked method when you click the Button
m_YourFirstButton.onClick.AddListener(TaskOnClick);
}
void TaskOnClick()
{
//Output this to console when Button1 or Button3 is clicked
Debug.Log("You have clicked the button!");
}
or you could call it from the inspector by referencing your Gun monobehaviour.

Make Sprite Draggable When Touched

Is there a way to make a sprite draggable but only when the sprite itself is touched? Currently i have my game ,which uses anengine, set up to where the sprite follows your finger ever time the scene is touched. If you touch the opposite side of the scene the sprite gets "teleported" which is what i dont want.
I tried overriding the onAreaTouched method of the sprite and made it set its coordinates to where your finger currently is but this doesnt work too well. If you make sudden movements the draggablity wears off.
is there any simple way to accomplish this?
Answering my own question... i used this code and it worked perfectly:
draggableSprite = new Sprite(CAM_WIDTH/2, CAM_HEIGHT/2,
spriteTextureRegion, mVertexBufferObjectManager){
#Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
float pTouchAreaLocalX, float pTouchAreaLocalY) {
if(pSceneTouchEvent.isActionMove()){
spriteIsTouched = true;
}
else{
spriteIsTouched = false;
}
return super
.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
}
};
scene.attachChild(draggableSprite);
scene.registerTouchArea(draggableSprite);
scene.setOnSceneTouchListener(new IOnSceneTouchListener() {
#Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
// TODO Auto-generated method stub
if(spriteIsTouched){
draggableSprite.setPosition(pSceneTouchEvent.getX() - (draggableSprite.getWidth()/2), pSceneTouchEvent.getY() - (draggableSprite.getHeight()/2));
//This sets the position of the sprite and then
//offsets the sprite so its center is at your finger
}
return false;
}
});`

Preserving object after a reload scene

In a scene of my quiz game I have an animation object that changes for another animation when a button to move to the next question is pressed (the button reload the scene). I would like to keep the animation, the last animation referenced to the object, after the scene is reloaded, but I don't know how. The object always returns to its normal state (the first animation).
I currently have a script called 'tower' referenced to the object where I make a static object and a DontDestroyOnLoad function:
public static SpriteRenderer towerAnimation;
void Awake()
{
DontDestroyOnLoad(gameObject);
towerAnimation = GetComponent<SpriteRenderer>();
}
And this code in the Update of 'GameManager' script:
public static int counterQuestionChances = 2;
void DestroyTower()
{
if (counterQuestionChances == 1)
{
Tower.towerAnimation.SetTrigger("error 1");
}
else
{
if (counterQuestionChances == 0)
{
Tower.towerAnimation.SetTrigger("error 2");
}
}
But it doesn't work. I'm taking shots in the dark because I don't know how to solve this. I'd appreciate if you could give me any ideas that can help me with this. Thanks!
You're going to have to use the SceneManagement library that Unity has so when the scene changes the method below called OnActiveSceneChanged gets called.
using UnityEngine.SceneManagement;
public class Tower : MonoBehaviour
{
public static SpriteRenderer towerAnimation;
public int storedCounter = 0;
void Awake()
{
DontDestroyOnLoad(gameObject);
towerAnimation = GetComponent<SpriteRenderer>();
SceneManager.activeSceneChanged += OnActiveSceneChanged;
}
private void OnActiveSceneChanged(UnityEngine.SceneManagement.Scene scene, UnityEngine.SceneManagement.Scene currentScene)
{
//tower animation stuff here
}
}