im tryng to ad a zoom event on my map which transform my heatmap in marker. I found this code and tryinn to apply it on my map
var points = L.geoJson('1999to2022.geojson', {
pointToLayer: function (feature, latlng) {
return L.marker(latlng, {});
} });
map.on('zoomend', function() { var zoomlevel = map.getZoom();
if (zoomlevel <6){
if (map.hasLayer(points)) {
map.removeLayer(points);
} else {
console.log("no point layer active");
}
}
if (zoomlevel >= 7){
if (map.hasLayer(points)){
console.log("layer already added");
} else {
map.addLayer(points);
}
} });
I was wondering what I could be doing wrong and if this was the easiest way to display my popup.
Thanks!
Related
While scrolling moveable columns cell renderer is getting re-initialized due to which the value in ag grid are getting reset. I need solution for this as I am not able to save row data as values gets reset on scrolling.
agInit(params): void {
this.params = params.data;
this.rowIndex = params.rowIndex;
this.selectedReason = params.data;
if (this.params.proposedCompletionDateInFormat) {
this.calender.selectedDateTime.startDate = new Date(params.data.proposedCompletionDateInFormat);
} else {
this.calender.selectedDateTime = null;
}
if(params.data.taskProgressState === "COMPLETED") {
this.disableField = true;
} else {
this.disableField = false;
}
}
refresh(params?: any): boolean {
this.formGroup = params.context.formGroup;
return true;
}
}
add
[suppressColumnVirtualisation]="true" flag
I would like a Unity progress slider to fill up when I press the spacebar. Currently, I have to mash the spacebar 100 times to fill up the progress bar.
It fills the progress slider automatically when moved to update().
It fills the progress slider when I hold the spacebar if I change Input.GetKeyDown to Input.GetKey.
But I don't want to hold the spacebar down. I want to press it once to start have the slider value gradually fill up the progress bar.
void Start()
{
sliderValue = 1.0f;
}
void Update()
{
pressSpacebar();
}
void pressSpacebar()
{
if (Input.GetKeyDown("space"))
{
filluptheslider();
}
}
void filluptheslider()
{
ProgressSlider.value += sliderValue * Time.deltaTime;
}
There are a few ways to handle this:
Method 1
Boolean value in the class to track the pressed state:
bool pressed = false;
void pressSpacebar()
{
if (Input.GetKeyDown("space"))
{
filluptheslider();
}
if(pressed)
{
filluptheslider();
}
}
Method 2
Coroutine that starts after space is pressed.
void pressSpacebar()
{
if (Input.GetKeyDown("space"))
{
StartCoroutine(FillUpTheSlider);
}
}
IEnumerator FillUpTheSlider()
{
while(ProgressSlider.value < ProgressSlider.maxValue)
{
ProgressSlider.value += sliderValue * Time.deltaTime;
yield return null;
}
}
If you press the space bar multiple times, multiple coroutines will spawn and it will fill faster. You can optionally add the following before you start the coroutine to prevent this behaviour:
StopCoroutine(FillUpTheSlider)
You could alternatively combine method 1 and 2 to remove this behaviour.
bool isFull = false;
bool isPressed = false;
void Update
{
If (Input.GetKeyDown(KeyCode.Space)
&&
!isFull)
{
isPressed = true;
}
if(isPressed)
{
//if you want fill to increase every 0.05 seconds.
StartCoroutine(FillGradually(0.05f));
isPressed = false;
}
}
Ienumerator FillGradually(float fillOffset)
{
//If 100 is your max value
while(ProgressSlider.value < 100)
{
ProgressSlider.value += 1 * Time.deltaTime;
yield return new WaitForSeconds(fillOffset);
}
isFull = true;
}
You can just use a bool condition and when space is pressed, you can just make it true once and when it is true, it will automatically start filling.
bool isSpacePressed; //bydefault, bool class variable is set to false //when initialized
update()
{
if(isSpacePressed==true) //no need to mention ==true just added to make it //readable
{
image.fillAmount=fillvalue+0.1f;
}
}
if(input.GetKey(keyCode.Space)==true)
{
isSpacePressed=true;
}
I'm making my own game in Unity 4.6. I have background music playing all the time. But i want to give my players the possibility to mute the music with a simple toggle.
I'm using C#. Anyone any ideas on how to do this?
Would be a great help!
this is how i solved it!
using UnityEngine;
using System.Collections;
public class Mute: MonoBehaviour{
bool isMute;
public void MusicMute (){
if(isMute == true){
Debug.Log("Music On");
AudioListener.volume = 1;
isMute = false;
}
else {
Debug.Log("Music Off");
isMute = true;
AudioListener.volume = 0;
}
}
}
Imagining that you are using a GUI Button to toggle...
You can toggle the volume by following:
void OnGUI ()
{
if (GUI.Button (new Rect (0, 0, 30, 30), "Music")) {
if (audio.volume != 0) {
audio.volume = 0;
} else {
audio.volume = 1; //or whatever value you want
}
}
}
Otherwise you can play or stop by following:
void OnGUI ()
{
if (GUI.Button (new Rect (0, 0, 30, 30), "Music")) {
if (audio.isPlaying) {
audio.Stop ();
} else {
audio.Play ();
}
}
}
Hope it helps.
I am trying to implement drag and drop functionality for my game in unity 2d. I have multiple copies of same object in my screen and they differ only by collider name. I attached the same script to them. Here is a piece of my code
function Start () {
playerTouches = [-1, -1];
}
function resetPlayer(touchNumber: int) {
for(var i = 0; i < playerTouches.length; ++i) {
if(touchNumber == playerTouches[i]) {
playerTouches[i] = -1;
}
}
}
function getCollider(vec: Vector2) {
var ray : Ray = Camera.main.ScreenPointToRay(vec);
var hit : RaycastHit2D = Physics2D.Raycast(ray.origin, ray.direction);
if (hit) {
if (hit.collider != null) {
Debug.Log(hit.collider.name);
return hit.collider.name;
} else {
Debug.Log("is null");
return "null";
}
} else {
Debug.Log("empty");
return "";
}
return "";
}
function processTouch(touch: Touch, touchNumber: int) {
if(touch.phase == TouchPhase.Began) {
var colliderName: String = getCollider(touch.position);
if(colliderName == "Object01" && playerTouches[0] == -1) {
playerTouches[0] = touchNumber;
} else if(colliderName == "Object02" && playerTouches[1] == -1) {
playerTouches[1] = touchNumber;
}
} else if(touch.phase == TouchPhase.Moved) {
// get object and change coords
} else if(touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) {
resetPlayer(touchNumber);
}
}
function Update() {
if(Input.touchCount > 0) {
//Debug.Log("count = " + Input.touchCount);
for(var i = 0; i < Input.touchCount; i++)
{
processTouch(Input.GetTouch(i), i);
//Debug.Log("touch : " + i + " " + Input.GetTouch(i).position);
}
}
}
For now I'm detecting on which object user touch. I need to be able to get that object and change it's position.
I also found this code snippet which allows to move rigidbody
var touchDeltaPosition: Vector2 = touch.deltaPosition;
var touchPosition: Vector2;
touchPosition.Set(touchDeltaPosition.x, touchDeltaPosition.y);
rigidbody2D.transform.position = Vector2.Lerp(transform.position, touchPosition, Time.deltaTime * spd);
but it moves all objects regardless of what object I select.
Well, you can do like this. If you have 12 copies of same object and want to move the object which selected by user. So when user Touches the object Change that GameObject tag or Name to another tag. Afterward you can use the some Conditional Statement to work with your code.
Example :
if(Input.GetMouseButtonDown(0)) {
Debug.Log("Mouse is down");
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo = new RaycastHit();
//bool hit = Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hitInfo);
if(Physics.Raycast(ray, out hitInfo, 30)) {
Debug.Log("Hit " + hitInfo.transform.gameObject.name);
if(hitInfo.transform.gameObject.tag == "Deselected") {
//Debug.Log ("It's working! Attaching MoveCube Script to game :" + hitInfo.transform.gameObject.tag);
findObject = GameObject.FindGameObjectsWithTag("Deselected");
foreach(GameObject go in findObject) {
//go.gameObject.renderer.material.color = Color.white;
go.GetComponent<MoveCube>().enabled = false;
if(hitInfo.transform.gameObject.name.Equals(go.gameObject.name)) {
//hitInfo.transform.renderer.material.color = Color.white;
hitInfo.transform.gameObject.GetComponent<MoveCube>().enabled = true;
changeTAG = true;
} else {
hitInfo.transform.gameObject.tag = "Deselected"
}
}
playerObject = GameObject.FindGameObjectsWithTag("Player");
foreach(GameObject game in playerObject) {
count++;
if(count == 1) {
hitInfo.transform.gameObject.tag = "Player";
}
if(count >= 1) {
game.gameObject.tag = "Deselected";
game.gameObject.GetComponent<MoveCube>().enabled = false;
//game.gameObject.renderer.material.color = Color.white;
}
}
if(changeTAG) {
hitInfo.transform.gameObject.tag = "Player";
/*if (hitInfo.transform.gameObject.GetComponent<Rigidbody> ()) {
Debug.Log ("RigidBody is already added Can't add another RigidBody");
hitInfo.transform.rigidbody.WakeUp ();
} else {
hitInfo.transform.gameObject.AddComponent<Rigidbody> ().useGravity = false;
// hitInfo.transform.gameObject.GetComponent<Rigidbody> ().WakeUp ();
}*/
changeTAG = false;
} else if(!changeTAG) {
hitInfo.transform.gameObject.tag = "Deselected";
}
} else {
Debug.Log("Not Working");
}
} else {
Debug.Log("No hit");
}
Debug.Log("Mouse is down");
}
The above code is for Change the tag for selected and deselected cube. After that you can easily identify the Selected gameObject and can move it where ever you want.
You can use this code in the Update function.
I'll start with an example
ImageButton {
defaultImageSource: "asset:///images/test_p.png"
pressedImageSource: "asset:///images/test_p_pressed.png"
attachedObjects: [
OrientationHandler { //gives "orientation"
onOrientationAboutToChange: {
if (orientation == UIOrientation.Landscape) {
defaultImageSource = "asset:///images/test_l.png"
pressedImageSource = "asset:///images/test_l_pressed.png"
} else {
defaultImageSource = "asset:///images/test_p.png"
pressedImageSource = "asset:///images/test_p_pressed.png"
}
}
}
]
}
Screen rotation works just fine, when it's landscape it uses _l, when it's portrait it uses _p image. The problem is, when I start app in landscape, it will show _p, not _l image (because it's default). How do I check orientation in onCreationCompleted?
I figured it out, it's an obvious one really. You put id to your OrientationHandler, and then you use it in onCreationCompleted.
ImageButton {
defaultImageSource: "asset:///images/test_p.png" //do I still need this?
pressedImageSource: "asset:///images/test_p_pressed.png"
attachedObjects: [
OrientationHandler { //gives "orientation"
id: orientationHandler //make id so you can call it
onOrientationAboutToChange: {
changeImages(orientation);
}
}
]
function changeImages(orientation){
if (orientation == UIOrientation.Landscape) {
defaultImageSource = "asset:///images/test_l.png"
pressedImageSource = "asset:///images/test_l_pressed.png"
} else {
defaultImageSource = "asset:///images/test_p.png"
pressedImageSource = "asset:///images/test_p_pressed.png"
}
}
onCreationCompleted: {
changeImages(orientationHandler.orientation); //call it here
}
}